From 6a88d734bdb4660945923c3f4a38c14e45847c2c Mon Sep 17 00:00:00 2001 From: kongjun Date: Tue, 29 Apr 2025 03:49:28 +0000 Subject: feat: store passwords in a local scope instead of as a buffer variable --- lua/encrypt/helpers.lua | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) (limited to 'lua/encrypt/helpers.lua') diff --git a/lua/encrypt/helpers.lua b/lua/encrypt/helpers.lua index 37a96b3..ff8baef 100644 --- a/lua/encrypt/helpers.lua +++ b/lua/encrypt/helpers.lua @@ -1,13 +1,18 @@ ----@return string -local function getPassword() - local password = vim.b["password"] - if password == nil then - password = vim.fn.inputsecret("Enter password: ") - vim.b["password"] = password +---@return function string +local getPasswordFactory = function() + local bufferPasswordMap = {} + + return function() + local key = string.format("%s", vim.fn.bufnr()) + if bufferPasswordMap[key] == nil then + password = vim.fn.inputsecret("Enter password: ") + bufferPasswordMap[key] = password + end + return bufferPasswordMap[key] end - return password end +local getPassword = getPasswordFactory() local ENCRYPTED_PREFIX = "# <<>>" return { getPassword = getPassword, ENCRYPTED_PREFIX = ENCRYPTED_PREFIX } -- cgit v1.2.3 From e6057b3c13f004477f4963f3cdf054e5ca1bf2f4 Mon Sep 17 00:00:00 2001 From: kongjun Date: Tue, 29 Apr 2025 04:05:35 +0000 Subject: feat: lock the encrypted file to read-only mode to avoid unintended corruption --- lua/encrypt/helpers.lua | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) (limited to 'lua/encrypt/helpers.lua') diff --git a/lua/encrypt/helpers.lua b/lua/encrypt/helpers.lua index ff8baef..a2ccc51 100644 --- a/lua/encrypt/helpers.lua +++ b/lua/encrypt/helpers.lua @@ -15,4 +15,10 @@ end local getPassword = getPasswordFactory() local ENCRYPTED_PREFIX = "# <<>>" -return { getPassword = getPassword, ENCRYPTED_PREFIX = ENCRYPTED_PREFIX } +---@enum buftype +local BUFTYPE = { + encrypted = 1, + plaintext = 2, +} + +return { getPassword = getPassword, ENCRYPTED_PREFIX = ENCRYPTED_PREFIX, BUFTYPE = BUFTYPE} -- cgit v1.2.3 From 872632670c31f682f3db01707468dbc12f154ab4 Mon Sep 17 00:00:00 2001 From: kongjun Date: Tue, 29 Apr 2025 06:51:01 +0000 Subject: feat: :X decrypts or encrypts files --- lua/encrypt/helpers.lua | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) (limited to 'lua/encrypt/helpers.lua') diff --git a/lua/encrypt/helpers.lua b/lua/encrypt/helpers.lua index a2ccc51..b740f28 100644 --- a/lua/encrypt/helpers.lua +++ b/lua/encrypt/helpers.lua @@ -21,4 +21,16 @@ local BUFTYPE = { plaintext = 2, } -return { getPassword = getPassword, ENCRYPTED_PREFIX = ENCRYPTED_PREFIX, BUFTYPE = BUFTYPE} +local function bufferEncrypted() + local first_line = vim.api.nvim_buf_get_lines(0, 0, 1, false)[1] + if first_line == ENCRYPTED_PREFIX then + return true + end +end + +return { + getPassword = getPassword, + ENCRYPTED_PREFIX = ENCRYPTED_PREFIX, + BUFTYPE = BUFTYPE, + bufferEncrypted = bufferEncrypted, +} -- cgit v1.2.3 From a9e7d700eb546800e3d341bc53667964ac821a38 Mon Sep 17 00:00:00 2001 From: kongjun Date: Tue, 6 May 2025 10:14:39 +0000 Subject: feat: handle the case where the inputsecret() is cancelled --- lua/encrypt/helpers.lua | 3 +++ 1 file changed, 3 insertions(+) (limited to 'lua/encrypt/helpers.lua') diff --git a/lua/encrypt/helpers.lua b/lua/encrypt/helpers.lua index b740f28..c88b448 100644 --- a/lua/encrypt/helpers.lua +++ b/lua/encrypt/helpers.lua @@ -6,6 +6,9 @@ local getPasswordFactory = function() local key = string.format("%s", vim.fn.bufnr()) if bufferPasswordMap[key] == nil then password = vim.fn.inputsecret("Enter password: ") + if password == "" then + vim.notify("Password is cancelled", vim.log.levels.WARN) + end bufferPasswordMap[key] = password end return bufferPasswordMap[key] -- cgit v1.2.3