diff --git a/lib/active_record_encryption/encryptor/active_support.rb b/lib/active_record_encryption/encryptor/active_support.rb index 7259380..bca0122 100644 --- a/lib/active_record_encryption/encryptor/active_support.rb +++ b/lib/active_record_encryption/encryptor/active_support.rb @@ -3,9 +3,9 @@ module ActiveRecordEncryption module Encryptor class ActiveSupport < Raw - def initialize(key:, salt:) + def initialize(key:, salt:, cipher: nil) key_generator = ::ActiveSupport::KeyGenerator.new(key) - @encryptor = ::ActiveSupport::MessageEncryptor.new(key_generator.generate_key(salt, 32)) + @encryptor = ::ActiveSupport::MessageEncryptor.new(key_generator.generate_key(salt, 32), cipher: cipher) end def encrypt(value) diff --git a/spec/active_record_encryption/encryptor/active_support_spec.rb b/spec/active_record_encryption/encryptor/active_support_spec.rb index b0fd46f..d826390 100644 --- a/spec/active_record_encryption/encryptor/active_support_spec.rb +++ b/spec/active_record_encryption/encryptor/active_support_spec.rb @@ -12,6 +12,30 @@ expect { described_class.new(key: key) }.to raise_error(ArgumentError) end end + + context 'with cipher' do + let(:instance) { described_class.new(salt: salt, key: key, cipher: cipher) } + + def new_cipher + instance.instance_variable_get(:@encryptor).send(:new_cipher) + end + + context 'cipher is "aes-256-gcm"' do + let(:cipher) { 'aes-256-gcm' } + + it 'builds aes-256-gcm encryptor' do + expect(new_cipher.name).to eq('id-aes256-GCM') + end + end + + context 'cipher is "aes-256-cbc"' do + let(:cipher) { 'aes-256-cbc' } + + it 'builds aes-256-cbc encryptor' do + expect(new_cipher.name).to eq('AES-256-CBC') + end + end + end end describe '#encrypt/decrypt' do