aboutsummaryrefslogtreecommitdiff
path: root/lua/encrypt/decrypt.lua
blob: de4901909e6f6dfc0d66956245ce6c4a1c01fb12 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
local helpers = require("encrypt.helpers")

---@return string[]
---@param lines string | string[]
---@param password string
local function decrypt_lines(lines, password)
  return vim.fn.systemlist(
    "base64 --decode | openssl enc -d -aes-256-cbc -pbkdf2 -salt -in - -out - -k " .. vim.fn.shellescape(password),
    lines
  )
end

local function decrypt()
  local password = helpers.getPassword()
  local encrypted_lines = vim.api.nvim_buf_get_lines(0, 1, -1, false)
  local decrypted_lines = decrypt_lines(encrypted_lines, password)
  if vim.v.shell_error ~= 0 then
    vim.notify("Error decrypting", vim.log.levels.ERROR)
    return
  end

  vim.fn.timer_start(0, function()
    vim.bo.modifiable = true
    vim.api.nvim_buf_set_lines(0, 0, -1, false, decrypted_lines)
    vim.bo.modified = false
  end)
end

return decrypt