Packages

Read and write encrypted files using openssl shell commands.

Current section

Files

Jump to
glm_encrypted_file src glm_encrypted_file@encfile.erl
Raw

src/glm_encrypted_file@encfile.erl

-module(glm_encrypted_file@encfile).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/glm_encrypted_file/encfile.gleam").
-export([new_plaintext_file/1, new_encrypted_file/1, new_password_file/1, decrypt/2, encrypt/3]).
-export_type([plaintext_file/0, password_file/0, encrypted_file/0, enc_file_error/0]).
-if(?OTP_RELEASE >= 27).
-define(MODULEDOC(Str), -moduledoc(Str)).
-define(DOC(Str), -doc(Str)).
-else.
-define(MODULEDOC(Str), -compile([])).
-define(DOC(Str), -compile([])).
-endif.
-type plaintext_file() :: {plaintext_file, binary()}.
-type password_file() :: {password_file, binary()}.
-type encrypted_file() :: {encrypted_file, binary()}.
-type enc_file_error() :: {decrypt_encrypted_file_error,
list(binary()),
{integer(), binary()}} |
{encrypt_encrypted_file_error, list(binary()), {integer(), binary()}}.
-file("src/glm_encrypted_file/encfile.gleam", 41).
?DOC(" Construct a plaintext file\n").
-spec new_plaintext_file(binary()) -> plaintext_file().
new_plaintext_file(F) ->
{plaintext_file, F}.
-file("src/glm_encrypted_file/encfile.gleam", 46).
?DOC(" Construct an encrypted file\n").
-spec new_encrypted_file(binary()) -> encrypted_file().
new_encrypted_file(F) ->
{encrypted_file, F}.
-file("src/glm_encrypted_file/encfile.gleam", 51).
?DOC(" Construct a password file\n").
-spec new_password_file(binary()) -> password_file().
new_password_file(F) ->
{password_file, F}.
-file("src/glm_encrypted_file/encfile.gleam", 62).
?DOC(" Decrypt an encrypted file\n").
-spec decrypt(encrypted_file(), password_file()) -> {ok, binary()} |
{error, enc_file_error()}.
decrypt(Encrypted_file, Password_file) ->
Command_arguments = begin
_pipe = [[<<"enc"/utf8>>,
<<"-base64"/utf8>>,
<<"-aes-256-cbc"/utf8>>,
<<"-md"/utf8>>,
<<"sha512"/utf8>>,
<<"-pbkdf2"/utf8>>,
<<"-iter"/utf8>>,
<<"1000000"/utf8>>],
[<<"-d"/utf8>>],
[<<"-in"/utf8>>,
erlang:element(2, Encrypted_file),
<<"-pass"/utf8>>,
<<"file:"/utf8, (erlang:element(2, Password_file))/binary>>]],
lists:append(_pipe)
end,
_pipe@1 = shellout:command(
<<"openssl"/utf8>>,
Command_arguments,
<<"."/utf8>>,
[]
),
_pipe@2 = gleam@result:map(
_pipe@1,
fun(Output) ->
logging:log(
debug,
<<<<<<"shell command succeeded: "/utf8, "openssl"/utf8>>/binary,
" "/utf8>>/binary,
(gleam@string:inspect(Command_arguments))/binary>>
),
Output
end
),
gleam@result:map_error(
_pipe@2,
fun(_capture) ->
{decrypt_encrypted_file_error, Command_arguments, _capture}
end
).
-file("src/glm_encrypted_file/encfile.gleam", 95).
?DOC(" Encrypt a plaintext file\n").
-spec encrypt(plaintext_file(), encrypted_file(), password_file()) -> {ok, nil} |
{error, enc_file_error()}.
encrypt(Plaintext_file, Encrypted_file, Password_file) ->
Command_arguments = begin
_pipe = [[<<"enc"/utf8>>,
<<"-base64"/utf8>>,
<<"-aes-256-cbc"/utf8>>,
<<"-md"/utf8>>,
<<"sha512"/utf8>>,
<<"-pbkdf2"/utf8>>,
<<"-iter"/utf8>>,
<<"1000000"/utf8>>],
[<<"-e"/utf8>>, <<"-salt"/utf8>>],
[<<"-in"/utf8>>,
erlang:element(2, Plaintext_file),
<<"-out"/utf8>>,
erlang:element(2, Encrypted_file),
<<"-pass"/utf8>>,
<<"file:"/utf8, (erlang:element(2, Password_file))/binary>>]],
lists:append(_pipe)
end,
_pipe@1 = shellout:command(
<<"openssl"/utf8>>,
Command_arguments,
<<"."/utf8>>,
[]
),
_pipe@2 = gleam@result:map(
_pipe@1,
fun(_) ->
logging:log(
debug,
<<<<<<"shell command succeeded: "/utf8, "openssl"/utf8>>/binary,
" "/utf8>>/binary,
(gleam@string:inspect(Command_arguments))/binary>>
),
nil
end
),
gleam@result:map_error(
_pipe@2,
fun(_capture) ->
{encrypt_encrypted_file_error, Command_arguments, _capture}
end
).