aboutsummaryrefslogtreecommitdiff
path: root/lua/encrypt/encrypt.lua
diff options
context:
space:
mode:
authorDavid Elentok <3david@gmail.com>2024-05-14 18:15:13 +0300
committerDavid Elentok <3david@gmail.com>2024-05-14 18:17:09 +0300
commita805d34f123bc8add01bd44ad28f0baf57710083 (patch)
treeb868cef404823eaf4b7c78316459cf8b2a3fd013 /lua/encrypt/encrypt.lua
downloadencrypt.nvim-a805d34f123bc8add01bd44ad28f0baf57710083.tar.gz
Initial commit
Diffstat (limited to 'lua/encrypt/encrypt.lua')
-rw-r--r--lua/encrypt/encrypt.lua25
1 files changed, 25 insertions, 0 deletions
diff --git a/lua/encrypt/encrypt.lua b/lua/encrypt/encrypt.lua
new file mode 100644
index 0000000..5cdf8a9
--- /dev/null
+++ b/lua/encrypt/encrypt.lua
@@ -0,0 +1,25 @@
+local helpers = require("encrypt.helpers")
+
+---@return string[]
+---@param lines string | string[]
+---@param password string
+local function encrypt_lines(lines, password)
+ return vim.fn.systemlist(
+ "openssl enc -aes-256-cbc -pbkdf2 -salt -in - -out - -k " .. password .. " | base64",
+ lines
+ )
+end
+
+local function encrypt()
+ local password = helpers.getPassword()
+ local buf_lines = vim.api.nvim_buf_get_lines(0, 0, -1, false)
+ local encrypted_lines = encrypt_lines(buf_lines, password)
+
+ table.insert(encrypted_lines, 1, helpers.ENCRYPTED_PREFIX)
+
+ vim.fn.writefile(encrypted_lines, vim.fn.expand("%"))
+ vim.bo.modified = false
+ print("Encrypted " .. vim.fn.expand("%:t"))
+end
+
+return encrypt