Skip to content

Commit

Permalink
fix: pubnub.decrypt() function to return correct data format
Browse files Browse the repository at this point in the history
  • Loading branch information
mohitpubnub committed Oct 17, 2023
1 parent fb6cd04 commit cedda37
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 5 deletions.
4 changes: 2 additions & 2 deletions dist/web/pubnub.js
Original file line number Diff line number Diff line change
Expand Up @@ -791,7 +791,7 @@
return this;
};
default_1.prototype.getVersion = function () {
return '7.4.0';
return '7.3.3';
};
default_1.prototype._addPnsdkSuffix = function (name, suffix) {
this._PNSDKSuffix[name] = suffix;
Expand Down Expand Up @@ -7843,7 +7843,7 @@
this.decrypt = function (data, key) {
if (typeof key === 'undefined' && cryptoModule) {
var decrypted = modules.cryptoModule.decrypt(data);
return decrypted instanceof ArrayBuffer ? encode$1(decrypted) : decrypted;
return decrypted instanceof ArrayBuffer ? JSON.parse(new TextDecoder().decode(decrypted)) : decrypted;
}
else {
return crypto.decrypt(data, key);
Expand Down
2 changes: 1 addition & 1 deletion dist/web/pubnub.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion lib/core/pubnub-common.js
Original file line number Diff line number Diff line change
Expand Up @@ -500,7 +500,7 @@ var default_1 = /** @class */ (function () {
this.decrypt = function (data, key) {
if (typeof key === 'undefined' && cryptoModule) {
var decrypted = modules.cryptoModule.decrypt(data);
return decrypted instanceof ArrayBuffer ? (0, base64_codec_1.encode)(decrypted) : decrypted;
return decrypted instanceof ArrayBuffer ? JSON.parse(new TextDecoder().decode(decrypted)) : decrypted;
}
else {
return crypto.decrypt(data, key);
Expand Down
2 changes: 1 addition & 1 deletion src/core/pubnub-common.js
Original file line number Diff line number Diff line change
Expand Up @@ -691,7 +691,7 @@ export default class {
this.decrypt = function (data, key) {
if (typeof key === 'undefined' && cryptoModule) {
const decrypted = modules.cryptoModule.decrypt(data);
return decrypted instanceof ArrayBuffer ? encode(decrypted) : decrypted;
return decrypted instanceof ArrayBuffer ? JSON.parse(new TextDecoder().decode(decrypted)) : decrypted;
} else {
return crypto.decrypt(data, key);
}
Expand Down
22 changes: 22 additions & 0 deletions test/integration/components/crypto.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,26 @@ describe('components/crypto useRandomIVs', () => {

expect(decrypted).to.deep.equal(data);
});

it('should be able to encrypt and decrypt a message with CryptoModule', () => {
const pubnub = new PubNub({
subscribeKey: 'demo-36',
publishKey: 'demo-36',
useRandomIVs: true,
cryptoModule: PubNub.CryptoModule.aesCbcCryptoModule({
cipherKey: 'abcd'
}),
uuid: 'myUUID',
});
const data = {
message:
'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.',
};
const plaintext = JSON.stringify(data);
const ciphertext = pubnub.encrypt(plaintext);

const decrypted = pubnub.decrypt(ciphertext);

expect(decrypted).to.deep.equal(data);
});
});

0 comments on commit cedda37

Please sign in to comment.