Skip to content

Commit 7e25fd0

Browse files
committed
Fix decryption IV
1 parent 70d4144 commit 7e25fd0

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

lib/encryption.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ def self.decrypt_sensitive_value(val = "")
1515
aes = OpenSSL::Cipher.new(cipher_type)
1616
aes.decrypt
1717
aes.key = key[0..31]
18-
aes.iv = iv[0.15] if iv != nil
18+
aes.iv = iv[0..15] if iv != nil
1919
decoded = Base64.strict_decode64("#{val}")
2020
aes.update("#{decoded}") + aes.final
2121
end

spec/lib/encryption_spec.rb

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# frozen_string_literal: true
2+
require "spec_helper"
3+
require_relative "../../lib/encryption"
4+
5+
describe Encryption do
6+
let(:value) {
7+
allow(Encryption).to receive(:key).and_return(SecureRandom.bytes(32))
8+
allow(Encryption).to receive(:iv).and_return(SecureRandom.bytes(16))
9+
10+
"OMG PII"
11+
}
12+
13+
it "encrypts values" do
14+
encrypted = Encryption.encrypt_sensitive_value(value)
15+
expect(Base64.decode64(encrypted)).not_to eq(value)
16+
end
17+
18+
it "decrypts values" do
19+
encrypted = Encryption.encrypt_sensitive_value(value)
20+
decrypted = Encryption.decrypt_sensitive_value(encrypted)
21+
22+
expect(decrypted).to eq(value)
23+
end
24+
end

0 commit comments

Comments
 (0)