We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent 060ee3a commit d1ae2e5Copy full SHA for d1ae2e5
2 files changed
lib/plug/crypto/message_encryptor.ex
@@ -34,7 +34,7 @@ defmodule Plug.Crypto.MessageEncryptor do
34
It defaults to "A128GCM" for backwards compatibility.
35
"""
36
def encrypt(message, aad \\ "A128GCM", secret, sign_secret)
37
- when is_binary(message) and is_binary(aad) and byte_size(secret) > 0 and
+ when is_binary(message) and (is_binary(aad) or is_list(aad)) and byte_size(secret) > 0 and
38
is_binary(sign_secret) do
39
aes128_gcm_encrypt(message, aad, secret, sign_secret)
40
rescue
@@ -45,7 +45,7 @@ defmodule Plug.Crypto.MessageEncryptor do
45
Decrypts a message using authenticated encryption.
46
47
def decrypt(encrypted, aad \\ "A128GCM", secret, sign_secret)
48
- when is_binary(encrypted) and is_binary(aad) and byte_size(secret) > 0 and
+ when is_binary(encrypted) and (is_binary(aad) or is_list(aad)) and byte_size(secret) > 0 and
49
50
aes128_gcm_decrypt(encrypted, aad, secret, sign_secret)
51
test/plug/crypto/message_encryptor_test.exs
@@ -11,7 +11,7 @@ defmodule Plug.Crypto.MessageEncryptorTest do
11
12
test "it encrypts/decrypts a message" do
13
data = <<0, "hełłoworld", 0>>
14
- encrypted = ME.encrypt(<<0, "hełłoworld", 0>>, "right aad", @right, @right)
+ encrypted = ME.encrypt(data, "right aad", @right, @right)
15
16
decrypted = ME.decrypt(encrypted, "right aad", @wrong, @wrong)
17
assert decrypted == :error
@@ -29,6 +29,12 @@ defmodule Plug.Crypto.MessageEncryptorTest do
29
assert decrypted == {:ok, data}
30
end
31
32
+ test "it encrypts/decrypts with iodata aad" do
33
+ data = <<0, "hełłoworld", 0>>
+ encrypted = ME.encrypt(data, ["right", ?\s, "aad"], @right, @right)
+ assert ME.decrypt(encrypted, ["right", ?\s, "aad"], @right, @right) == {:ok, data}
+ end
+
test "it uses only the first 32 bytes to encrypt/decrypt" do
data = <<0, "helloworld", 0>>
encrypted = ME.encrypt(<<0, "helloworld", 0>>, @large, @large)
0 commit comments