Skip to content

Commit aec75b5

Browse files
authored
Set default cipher to aes-256-gcm (#36)
1 parent 0802c40 commit aec75b5

File tree

3 files changed

+16
-9
lines changed

3 files changed

+16
-9
lines changed

lib/diffcrypt/encryptor.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
module Diffcrypt
1414
class Encryptor
15-
DEFAULT_CIPHER = 'aes-128-gcm'
15+
DEFAULT_CIPHER = 'aes-256-gcm'
1616

1717
def self.generate_key(cipher = DEFAULT_CIPHER)
1818
SecureRandom.hex(ActiveSupport::MessageEncryptor.key_len(cipher))

test/diffcrypt/encryptor_test.rb

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,13 @@ class Diffcrypt::EncryptorTest < Minitest::Test
1111
def test_it_includes_client_info_at_root
1212
content = "---\nkey: value"
1313
expected_pattern = /---\nclient: diffcrypt-#{Diffcrypt::VERSION}\ncipher: #{Diffcrypt::Encryptor::DEFAULT_CIPHER}\ndata:\n key: #{ENCRYPTED_VALUE_PATTERN}\n/
14-
assert_match expected_pattern, Diffcrypt::Encryptor.new(TEST_KEY).encrypt(content)
14+
assert_match expected_pattern, Diffcrypt::Encryptor.new(TEST_KEY_256).encrypt(content)
15+
end
16+
17+
def test_it_includes_cipher_when_not_default
18+
content = "---\nkey: value"
19+
expected_pattern = /---\nclient: diffcrypt-#{Diffcrypt::VERSION}\ncipher: aes-128-gcm\ndata:\n key: #{ENCRYPTED_VALUE_PATTERN}\n/
20+
assert_match expected_pattern, Diffcrypt::Encryptor.new(TEST_KEY_128, cipher: 'aes-128-gcm').encrypt(content)
1521
end
1622

1723
def test_it_decrypts_root_values
@@ -24,7 +30,7 @@ def test_it_decrypts_root_values
2430
secret_key_base: secret_key_base_test
2531
CONTENT
2632

27-
assert_equal Diffcrypt::Encryptor.new(TEST_KEY).decrypt(encrypted_content), expected
33+
assert_equal Diffcrypt::Encryptor.new(TEST_KEY_128, cipher: 'aes-128-gcm').decrypt(encrypted_content), expected
2834
end
2935

3036
def test_it_encrypts_root_values
@@ -34,7 +40,7 @@ def test_it_encrypts_root_values
3440
CONTENT
3541
expected_pattern = /---\nsecret_key_base: #{ENCRYPTED_VALUE_PATTERN}\n/
3642

37-
assert_match expected_pattern, Diffcrypt::Encryptor.new(TEST_KEY).encrypt_data(content).to_yaml
43+
assert_match expected_pattern, Diffcrypt::Encryptor.new(TEST_KEY_128, cipher: 'aes-128-gcm').encrypt_data(content).to_yaml
3844
end
3945

4046
def test_it_decrypts_nested_structures
@@ -51,7 +57,7 @@ def test_it_decrypts_nested_structures
5157
access_key_id: AKIAXXX
5258
CONTENT
5359

54-
assert_equal Diffcrypt::Encryptor.new(TEST_KEY).decrypt(encrypted_content), expected
60+
assert_equal Diffcrypt::Encryptor.new(TEST_KEY_128, cipher: 'aes-128-gcm').decrypt(encrypted_content), expected
5561
end
5662

5763
def test_it_encrypts_nested_structures
@@ -63,7 +69,7 @@ def test_it_encrypts_nested_structures
6369
CONTENT
6470
expected_pattern = /---\nsecret_key_base: #{ENCRYPTED_VALUE_PATTERN}\naws:\n access_key_id: #{ENCRYPTED_VALUE_PATTERN}\n/
6571

66-
assert_match expected_pattern, Diffcrypt::Encryptor.new(TEST_KEY).encrypt_data(content).to_yaml
72+
assert_match expected_pattern, Diffcrypt::Encryptor.new(TEST_KEY_128, cipher: 'aes-128-gcm').encrypt_data(content).to_yaml
6773
end
6874

6975
# Verifies that a change to one key does not cause the encrypted values for other keys to be recomputed
@@ -73,14 +79,14 @@ def test_it_only_updates_changed_values
7379
updated_content = "---\nsecret_key_base_1: secret_key_base_test\naws:\n secret_access_key: secret_access_key_2"
7480
expected_pattern = /---\nsecret_key_base_1: 88Ry6HESUoXBr6QUFXmni9zzfCIYt9qGNFvIWFcN--4xoecI5mqbNRBibI--62qPJbkzzh5h8lhFEFOSaQ==\naws:\n secret_access_key: #{ENCRYPTED_VALUE_PATTERN}\n/
7581

76-
assert_match expected_pattern, Diffcrypt::Encryptor.new(TEST_KEY).encrypt_data(updated_content, original_encrypted_content).to_yaml
82+
assert_match expected_pattern, Diffcrypt::Encryptor.new(TEST_KEY_128, cipher: 'aes-128-gcm').encrypt_data(updated_content, original_encrypted_content).to_yaml
7783
end
7884

7985
def test_it_assumes_changed_when_no_original_value
8086
original_encrypted_content = "---\ndata:\n secret_key_base_1: 88Ry6HESUoXBr6QUFXmni9zzfCIYt9qGNFvIWFcN--4xoecI5mqbNRBibI--62qPJbkzzh5h8lhFEFOSaQ==\n"
8187
updated_content = "---\nsecret_key_base_1: secret_key_base_test\naws:\n access_key_id: new_value\n"
8288
expected_pattern = /---\nsecret_key_base_1: 88Ry6HESUoXBr6QUFXmni9zzfCIYt9qGNFvIWFcN--4xoecI5mqbNRBibI--62qPJbkzzh5h8lhFEFOSaQ==\naws:\n access_key_id: #{ENCRYPTED_VALUE_PATTERN}\n/
8389

84-
assert_match expected_pattern, Diffcrypt::Encryptor.new(TEST_KEY).encrypt_data(updated_content, original_encrypted_content).to_yaml
90+
assert_match expected_pattern, Diffcrypt::Encryptor.new(TEST_KEY_128, cipher: 'aes-128-gcm').encrypt_data(updated_content, original_encrypted_content).to_yaml
8591
end
8692
end

test/test_helper.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#
2222
# @example Generate an expected value for tests
2323
# Diffcrypt::Encryptor.new('99e1f86b9e61f24c56ff4108dd415091').encrypt_string('some value here')
24-
TEST_KEY = ::File.read("#{__dir__}/fixtures/aes-128-gcm.key").strip
24+
TEST_KEY_128 = ::File.read("#{__dir__}/fixtures/aes-128-gcm.key").strip
25+
TEST_KEY_256 = ::File.read("#{__dir__}/fixtures/aes-256-gcm.key").strip
2526

2627
require 'minitest/autorun'

0 commit comments

Comments
 (0)