Skip to content

Commit

Permalink
Remove node 0.8.x specific code from node platform crypto
Browse files Browse the repository at this point in the history
  • Loading branch information
VeskeR committed Feb 27, 2024
1 parent 9a05dc8 commit 2fc88a3
Showing 1 changed file with 5 additions and 14 deletions.
19 changes: 5 additions & 14 deletions src/platform/nodejs/lib/util/crypto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,15 +76,6 @@ var createCryptoClass = function (bufferUtils: typeof BufferUtils) {
var pkcs5Padding = [filledBuffer(16, 16)];
for (var i = 1; i <= 16; i++) pkcs5Padding.push(filledBuffer(i, i));

/**
* Internal: convert a binary string to Buffer (for node 0.8.x)
* @param bufferOrString
* @returns {Buffer}
*/
function toBuffer(bufferOrString: Buffer | string) {
return typeof bufferOrString == 'string' ? Buffer.from(bufferOrString, 'binary') : bufferOrString;
}

/**
* A class encapsulating the client-specifiable parameters for
* the cipher.
Expand Down Expand Up @@ -236,19 +227,19 @@ var createCryptoClass = function (bufferUtils: typeof BufferUtils) {
var cipherOut = this.encryptCipher.update(
Buffer.concat([plaintextBuffer, pkcs5Padding[paddedLength - plaintextLength]])
);
var ciphertext = Buffer.concat([iv, toBuffer(cipherOut)]);
var ciphertext = Buffer.concat([iv, cipherOut]);
return ciphertext;
}

async decrypt(ciphertext: InputCiphertext): Promise<OutputPlaintext> {
var decryptCipher = crypto.createDecipheriv(this.algorithm, this.key, ciphertext.slice(0, DEFAULT_BLOCKLENGTH)),
plaintext = toBuffer(decryptCipher.update(ciphertext.slice(DEFAULT_BLOCKLENGTH))),
plaintext = decryptCipher.update(ciphertext.slice(DEFAULT_BLOCKLENGTH)),
final = decryptCipher.final();
if (final && final.length) plaintext = Buffer.concat([plaintext, toBuffer(final)]);
if (final && final.length) plaintext = Buffer.concat([plaintext, final]);
return plaintext;
}

async getIv() {
async getIv(): Promise<Buffer> {
if (this.iv) {
var iv = this.iv;
this.iv = null;
Expand All @@ -263,7 +254,7 @@ var createCryptoClass = function (bufferUtils: typeof BufferUtils) {
/* Since the iv for a new block is the ciphertext of the last, this
* sets a new iv (= aes(randomBlock XOR lastCipherText)) as well as
* returning it */
return toBuffer(this.encryptCipher.update(randomBlock));
return this.encryptCipher.update(randomBlock);
}
}
}
Expand Down

0 comments on commit 2fc88a3

Please sign in to comment.