Skip to content
This repository was archived by the owner on Feb 28, 2024. It is now read-only.

Commit c4191be

Browse files
authored
Merge pull request #4 from Mastercard/feature/decrypt_a_primitive_type_value
Decrypt a primitive type value to the same path than the encrypted data fields.
2 parents 387a759 + 2c4bd52 commit c4191be

File tree

2 files changed

+27
-3
lines changed

2 files changed

+27
-3
lines changed

lib/mcapi/encryption/field_level_encryption.rb

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,10 +99,15 @@ def encrypt_with_header(path, enc_params, header, body)
9999
def decrypt_with_body(path, body)
100100
elem = elem_from_path(path['element'], body)
101101
return unless elem && elem[:node]
102+
decrypted = @crypto.decrypt_data(elem[:node][@config['encryptedValueFieldName']],
103+
elem[:node][@config['ivFieldName']],
104+
elem[:node][@config['encryptedKeyFieldName']])
105+
begin
106+
decrypted = JSON.parse(decrypted)
107+
rescue JSON::ParserError
108+
# ignored
109+
end
102110

103-
decrypted = JSON.parse(@crypto.decrypt_data(elem[:node][@config['encryptedValueFieldName']],
104-
elem[:node][@config['ivFieldName']],
105-
elem[:node][@config['encryptedKeyFieldName']]))
106111
McAPI::Utils.mutate_obj_prop(path['obj'], decrypted, body, path['element'], @encryption_response_properties)
107112
end
108113

test/test_payload_encryption.rb

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,4 +125,23 @@ def test_decrypt_nested_properties_create_new_obj
125125
assert_equal body['path']['to']['foo']['accountNumber'], '5123456789012345'
126126
assert !body['path']['to']['encryptedFoo']
127127
end
128+
129+
def test_decrypt_primitive_type
130+
fle = McAPI::Encryption::FieldLevelEncryption.new(@test_config)
131+
body = JSON.parse(JSON.generate(
132+
data: {
133+
encryptedValue:
134+
'982f5df1dfff3cc551a49091140cbb22',
135+
iv: '6f38f3ecd8b92c2fd2537a7235deb9a8',
136+
encryptedKey:
137+
'aebc427a2ecefc748f7f42e34a1ea6592ea20a299c107df5655483bd6ae11f9de72cb407ea0342bcac7cc29e7e9bbfbfadf8209c1f7ae2429d6f8a914d161c8a91890b3d0363b1bdd80be64712fdd6ea35496649be05e0f87001185dd79fe7a7e23c716348afe27500aaacc2cbba89793a437ae5103170fd04f0e5b4f73089c0660b44506780346e84bfb6de183f15f49132bafac651f4b4e1fadc55205d3773877c4de02c0825d7ee0b44fd3f8bb4382b999237e190352e9199eceb209fe4a91ab88fa9b4988a1a9265be38582667784da83a8ec7c307e027884d49e76771dc4a4fb472e771324f72d24299f8f621d9501d6fd59de08ccce39f533a15e17022',
138+
publicKeyFingerprint:
139+
'80810fc13a8319fcf0e2ec322c82a4c304b782cc3ce671176343cfe8160c2279',
140+
oaepHashingAlgorithm: 'SHA512'
141+
}
142+
))
143+
fle.send :decrypt_with_body, JSON.parse(JSON.generate(element: 'data', obj: 'data')), body
144+
assert body['data']
145+
assert_equal body['data'], 'string'
146+
end
128147
end

0 commit comments

Comments
 (0)