This repository has been archived by the owner on Aug 2, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
decrypt_spec.rb
62 lines (53 loc) · 2.28 KB
/
decrypt_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# frozen_string_literal: true
require 'aes256gcm_decrypt'
require 'base64'
describe 'Aes256GcmDecrypt::decrypt' do
before(:each) do
@ciphertext_and_tag = Base64.decode64(File.read('spec/token_data_base64.txt'))
@key = [File.read('spec/key_hex.txt').strip].pack('H*')
end
it 'decrypts correctly' do
plaintext = Aes256GcmDecrypt.decrypt(@ciphertext_and_tag, @key)
expect(plaintext).to eq \
'{"applicationPrimaryAccountNumber":"4109370251004320","applicationExp' \
'irationDate":"200731","currencyCode":"840","transactionAmount":100,"d' \
'eviceManufacturerIdentifier":"040010030273","paymentDataType":"3DSecu' \
're","paymentData":{"onlinePaymentCryptogram":"Af9x/QwAA/DjmU65oyc1MAA' \
'BAAA=","eciIndicator":"5"}}'
end
it 'detects wrong parameter types' do
expect { Aes256GcmDecrypt.decrypt(42, @key) }.to \
raise_error(Aes256GcmDecrypt::InputError, 'ciphertext_and_tag must be a string')
expect { Aes256GcmDecrypt.decrypt(@ciphertext_and_tag, 42) }.to \
raise_error(Aes256GcmDecrypt::InputError, 'key must be a string')
end
it 'detects too short ciphertext_and_tag' do
(0..16).each do |i|
ciphertext_and_tag = 'x' * i
expect { Aes256GcmDecrypt.decrypt(ciphertext_and_tag, @key) }.to \
raise_error(Aes256GcmDecrypt::InputError, 'ciphertext_and_tag too short')
end
end
it 'detects wrong key length' do
((0..64).to_a - [32]).each do |i|
key = 'x' * i
expect { Aes256GcmDecrypt.decrypt(@ciphertext_and_tag, key) }.to \
raise_error(Aes256GcmDecrypt::InputError, 'length of key must be 32')
end
end
it 'detects tampering with the ciphertext' do
@ciphertext_and_tag[0] = 'x'
expect { Aes256GcmDecrypt.decrypt(@ciphertext_and_tag, @key) }.to \
raise_error(Aes256GcmDecrypt::AuthenticationError, 'Authentication failed')
end
it 'detects an incorrect tag' do
@ciphertext_and_tag[-1] = 'x'
expect { Aes256GcmDecrypt.decrypt(@ciphertext_and_tag, @key) }.to \
raise_error(Aes256GcmDecrypt::AuthenticationError, 'Authentication failed')
end
it 'detects an incorrect key' do
@key[0] = 'x'
expect { Aes256GcmDecrypt.decrypt(@ciphertext_and_tag, @key) }.to \
raise_error(Aes256GcmDecrypt::AuthenticationError, 'Authentication failed')
end
end