Skip to content

Commit 27580ff

Browse files
authored
Merge pull request #8 from Mastercard/feature/decrypt_a_primitive_type_value
Decrypt a primitive type value to the same path than the encrypted data fields.
2 parents eaf2e67 + ad2783f commit 27580ff

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

lib/mcapi/crypto/crypto.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,11 @@ function Crypto(config) {
9999
decipher.update(forge.util.createBuffer(utils.stringToBytes(encryptedData, this.encoding), 'binary'));
100100
decipher.finish();
101101

102-
return JSON.parse(decipher.output.data);
102+
try {
103+
return JSON.parse(decipher.output.data);
104+
} catch (e){
105+
return decipher.output.data;
106+
}
103107
};
104108

105109
/**

test/payload-encryption.test.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,27 @@ describe("Payload encryption", () => {
161161
assert.ok(body.path.to.foo);
162162
assert.ok(body.path.to.foo.accountNumber === "5123456789012345");
163163
});
164+
165+
it("primitive type", () => {
166+
let body = {
167+
data: {
168+
encryptedValue:
169+
'e2d6a3a76ea6e605e55b400e5a4eba11',
170+
iv: '3ce861359fa1630c7a794901ee14bf41',
171+
encryptedKey:
172+
'02bb8d5c7d113ef271f199c09f0d76db2b6d5d2d209ad1a20dbc4dd0d04576a92ceb917eea5f403ccf64c3c39dda564046909af96c82fad62f89c3cbbec880ea3105a0a171af904cd3b86ea68991202a2795dca07050ca58252701b7ecea06055fd43e96f4beee48b6275e86af93c88c21994ff46f0610171bd388a2c0a1f518ffc8346f7f513f3283feae5b102c8596ddcb2aea5e62ceb17222e646c599f258463405d28ac012bfd4cc431f94111ee07d79e660948485e38c13cdb8bba8e1df3f7dba0f4c77696f71930533c955f3a430658edaa03b0b0c393934d60f5ac3ea5c06ed64bf969fc01942eac432b8e0c56f7538659a72859d445d150c169ae690',
173+
publicKeyFingerprint:
174+
'761b003c1eade3a5490e5000d37887baa5e6ec0e226c07706e599451fc032a79',
175+
oaepHashingAlgorithm: 'SHA256'
176+
}
177+
};
178+
decryptBody.call(fle, {
179+
element: "data",
180+
obj: "data"
181+
}, body);
182+
assert.ok(body);
183+
assert.ok(body.data === 'string');
184+
});
164185
});
165186

166187
});

0 commit comments

Comments
 (0)