Skip to content

Commit

Permalink
refactor: crypto initialiser apis default value initialisation
Browse files Browse the repository at this point in the history
  • Loading branch information
mohitpubnub committed Oct 15, 2023
1 parent c17fca3 commit 915d355
Show file tree
Hide file tree
Showing 8 changed files with 61 additions and 15 deletions.
14 changes: 12 additions & 2 deletions dist/web/pubnub.js
Original file line number Diff line number Diff line change
Expand Up @@ -13080,15 +13080,25 @@
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
//@ts-ignore: type detection issue with old Config type assignment
CryptoModule.legacyCryptoModule = function (config) {
var _a;
return new this({
default: new LegacyCryptor(config),
default: new LegacyCryptor({
cipherKey: config.cipherKey,
useRandomIVs: (_a = config.useRandomIVs) !== null && _a !== void 0 ? _a : true,
}),
cryptors: [new AesCbcCryptor({ cipherKey: config.cipherKey })],
});
};
CryptoModule.aesCbcCryptoModule = function (config) {
var _a;
return new this({
default: new AesCbcCryptor({ cipherKey: config.cipherKey }),
cryptors: [new LegacyCryptor(config)],
cryptors: [
new LegacyCryptor({
cipherKey: config.cipherKey,
useRandomIVs: (_a = config.useRandomIVs) !== null && _a !== void 0 ? _a : true,
}),
],
});
};
CryptoModule.withDefaultCryptor = function (defaultCryptor) {
Expand Down
2 changes: 1 addition & 1 deletion dist/web/pubnub.min.js

Large diffs are not rendered by default.

16 changes: 13 additions & 3 deletions lib/crypto/modules/NodeCryptoModule/NodeCryptoModule.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,15 +80,25 @@ var CryptoModule = /** @class */ (function () {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore: type detection issue with old Config type assignment
CryptoModule.legacyCryptoModule = function (config) {
var _a;
return new this({
default: new legacyCryptor_1.default(config),
default: new legacyCryptor_1.default({
cipherKey: config.cipherKey,
useRandomIVs: (_a = config.useRandomIVs) !== null && _a !== void 0 ? _a : true,
}),
cryptors: [new aesCbcCryptor_1.default({ cipherKey: config.cipherKey })],
});
};
CryptoModule.aesCbcCryptoModule = function (config) {
var _a;
return new this({
default: new aesCbcCryptor_1.default({ cipherKey: config.cipherKey }),
cryptors: [new legacyCryptor_1.default(config)],
cryptors: [
new legacyCryptor_1.default({
cipherKey: config.cipherKey,
useRandomIVs: (_a = config.useRandomIVs) !== null && _a !== void 0 ? _a : true,
}),
],
});
};
CryptoModule.withDefaultCryptor = function (defaultCryptor) {
Expand All @@ -110,7 +120,7 @@ var CryptoModule = /** @class */ (function () {
headerData.set(header.data, pos);
pos = header.length - encrypted.metadata.length;
headerData.set(encrypted.metadata, pos);
return Buffer.concat([headerData, Buffer.from(encrypted.data)]).buffer;
return Buffer.concat([headerData, Buffer.from(encrypted.data)]);
};
CryptoModule.prototype.decrypt = function (data) {
var encryptedData = Buffer.from(typeof data === 'string' ? (0, base64_codec_1.decode)(data) : data);
Expand Down
2 changes: 1 addition & 1 deletion lib/crypto/modules/NodeCryptoModule/aesCbcCryptor.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ var AesCbcCryptor = /** @class */ (function () {
if (data.byteLength <= 0)
throw new Error('decryption error: empty content');
var aes = (0, crypto_1.createDecipheriv)(this.algo, this.getKey(), encryptedData.metadata);
return Uint8Array.from(Buffer.concat([aes.update(data), aes.final()]));
return Uint8Array.from(Buffer.concat([aes.update(data), aes.final()])).buffer;
};
AesCbcCryptor.prototype.encryptStream = function (stream) {
return __awaiter(this, void 0, void 0, function () {
Expand Down
14 changes: 12 additions & 2 deletions lib/crypto/modules/WebCryptoModule/webCryptoModule.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,15 +79,25 @@ var CryptoModule = /** @class */ (function () {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
//@ts-ignore: type detection issue with old Config type assignment
CryptoModule.legacyCryptoModule = function (config) {
var _a;
return new this({
default: new legacyCryptor_1.default(config),
default: new legacyCryptor_1.default({
cipherKey: config.cipherKey,
useRandomIVs: (_a = config.useRandomIVs) !== null && _a !== void 0 ? _a : true,
}),
cryptors: [new aesCbcCryptor_1.default({ cipherKey: config.cipherKey })],
});
};
CryptoModule.aesCbcCryptoModule = function (config) {
var _a;
return new this({
default: new aesCbcCryptor_1.default({ cipherKey: config.cipherKey }),
cryptors: [new legacyCryptor_1.default(config)],
cryptors: [
new legacyCryptor_1.default({
cipherKey: config.cipherKey,
useRandomIVs: (_a = config.useRandomIVs) !== null && _a !== void 0 ? _a : true,
}),
],
});
};
CryptoModule.withDefaultCryptor = function (defaultCryptor) {
Expand Down
2 changes: 1 addition & 1 deletion src/crypto/modules/NodeCryptoModule/aesCbcCryptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export default class AesCbcCryptor implements ICryptor {
typeof encryptedData.data === 'string' ? new TextEncoder().encode(encryptedData.data) : encryptedData.data;
if (data.byteLength <= 0) throw new Error('decryption error: empty content');
const aes = createDecipheriv(this.algo, this.getKey(), encryptedData.metadata);
return Uint8Array.from(Buffer.concat([aes.update(data), aes.final()]));
return Uint8Array.from(Buffer.concat([aes.update(data), aes.final()])).buffer;
}

async encryptStream(stream: NodeJS.ReadableStream) {
Expand Down
14 changes: 11 additions & 3 deletions src/crypto/modules/NodeCryptoModule/nodeCryptoModule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,23 @@ export class CryptoModule {
// @ts-ignore: type detection issue with old Config type assignment
static legacyCryptoModule(config) {
return new this({
default: new LegacyCryptor(config),
default: new LegacyCryptor({
cipherKey: config.cipherKey,
useRandomIVs: config.useRandomIVs ?? true,
}),
cryptors: [new AesCbcCryptor({ cipherKey: config.cipherKey })],
});
}

static aesCbcCryptoModule(config: any) {
return new this({
default: new AesCbcCryptor({ cipherKey: config.cipherKey }),
cryptors: [new LegacyCryptor(config)],
cryptors: [
new LegacyCryptor({
cipherKey: config.cipherKey,
useRandomIVs: config.useRandomIVs ?? true,
}),
],
});
}
static withDefaultCryptor(defaultCryptor: CryptorType) {
Expand All @@ -63,7 +71,7 @@ export class CryptoModule {
headerData.set(header!.data, pos);
pos = header!.length - encrypted.metadata.length;
headerData.set(encrypted.metadata, pos);
return Buffer.concat([headerData, Buffer.from(encrypted.data)]).buffer;
return Buffer.concat([headerData, Buffer.from(encrypted.data)]);
}

decrypt(data: ArrayBuffer | string) {
Expand Down
12 changes: 10 additions & 2 deletions src/crypto/modules/WebCryptoModule/webCryptoModule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,23 @@ export class CryptoModule {
//@ts-ignore: type detection issue with old Config type assignment
static legacyCryptoModule(config) {
return new this({
default: new LegacyCryptor(config),
default: new LegacyCryptor({
cipherKey: config.cipherKey,
useRandomIVs: config.useRandomIVs ?? true,
}),
cryptors: [new AesCbcCryptor({ cipherKey: config.cipherKey })],
});
}

static aesCbcCryptoModule(config: any) {
return new this({
default: new AesCbcCryptor({ cipherKey: config.cipherKey }),
cryptors: [new LegacyCryptor(config)],
cryptors: [
new LegacyCryptor({
cipherKey: config.cipherKey,
useRandomIVs: config.useRandomIVs ?? true,
}),
],
});
}

Expand Down

0 comments on commit 915d355

Please sign in to comment.