From a805d34f123bc8add01bd44ad28f0baf57710083 Mon Sep 17 00:00:00 2001 From: David Elentok <3david@gmail.com> Date: Tue, 14 May 2024 18:15:13 +0300 Subject: Initial commit --- lua/encrypt/encrypt.lua | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 lua/encrypt/encrypt.lua (limited to 'lua/encrypt/encrypt.lua') 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 -- cgit v1.2.3