mirror of
https://github.com/nodejs/node.git
synced 2026-10-14 10:26:26 -04:00
crypto: refactor to use more primordials
PR-URL: https://github.com/nodejs/node/pull/36012 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Rich Trott <rtrott@gmail.com>
This commit is contained in:
1 parent
ab587ca46b
commit
da53a3caa3
6 files changed
+29
-18
No files matched your search
@@ -1,6 +1,7 @@
|
||||
'use strict';
|
||||
|
||||
const {
|
||||
ArrayBufferPrototypeSlice,
|
||||
ArrayFrom,
|
||||
ArrayPrototypeIncludes,
|
||||
ArrayPrototypePush,
|
||||
@@ -182,8 +183,8 @@ function asyncAesGcmCipher(
|
||||
let tag;
|
||||
switch (mode) {
|
||||
case kWebCryptoCipherDecrypt:
|
||||
tag = data.slice(-tagByteLength);
|
||||
data = data.slice(0, -tagByteLength);
|
||||
tag = ArrayBufferPrototypeSlice(data, -tagByteLength);
|
||||
data = ArrayBufferPrototypeSlice(data, 0, -tagByteLength);
|
||||
break;
|
||||
case kWebCryptoCipherEncrypt:
|
||||
tag = tagByteLength;
|
||||
|
||||
@@ -2,6 +2,8 @@
|
||||
|
||||
const {
|
||||
ObjectSetPrototypeOf,
|
||||
ReflectApply,
|
||||
StringPrototypeToLowerCase,
|
||||
} = primordials;
|
||||
|
||||
const {
|
||||
@@ -118,14 +120,14 @@ function createCipherBase(cipher, credential, options, decipher, iv) {
|
||||
}
|
||||
this._decoder = null;
|
||||
|
||||
LazyTransform.call(this, options);
|
||||
ReflectApply(LazyTransform, this, [options]);
|
||||
}
|
||||
|
||||
function createCipher(cipher, password, options, decipher) {
|
||||
validateString(cipher, 'cipher');
|
||||
password = getArrayBufferOrView(password, 'password');
|
||||
|
||||
createCipherBase.call(this, cipher, password, options, decipher);
|
||||
ReflectApply(createCipherBase, this, [cipher, password, options, decipher]);
|
||||
}
|
||||
|
||||
function createCipherWithIV(cipher, key, options, decipher, iv) {
|
||||
@@ -133,7 +135,7 @@ function createCipherWithIV(cipher, key, options, decipher, iv) {
|
||||
const encoding = getStringOption(options, 'encoding');
|
||||
key = prepareSecretKey(key, encoding);
|
||||
iv = iv === null ? null : getArrayBufferOrView(iv, 'iv');
|
||||
createCipherBase.call(this, cipher, key, options, decipher, iv);
|
||||
ReflectApply(createCipherBase, this, [cipher, key, options, decipher, iv]);
|
||||
}
|
||||
|
||||
// The Cipher class is part of the legacy Node.js crypto API. It exposes
|
||||
@@ -145,7 +147,7 @@ function Cipher(cipher, password, options) {
|
||||
if (!(this instanceof Cipher))
|
||||
return new Cipher(cipher, password, options);
|
||||
|
||||
createCipher.call(this, cipher, password, options, true);
|
||||
ReflectApply(createCipher, this, [cipher, password, options, true]);
|
||||
}
|
||||
|
||||
ObjectSetPrototypeOf(Cipher.prototype, LazyTransform.prototype);
|
||||
@@ -241,7 +243,7 @@ function Cipheriv(cipher, key, iv, options) {
|
||||
if (!(this instanceof Cipheriv))
|
||||
return new Cipheriv(cipher, key, iv, options);
|
||||
|
||||
createCipherWithIV.call(this, cipher, key, options, true, iv);
|
||||
ReflectApply(createCipherWithIV, this, [cipher, key, options, true, iv]);
|
||||
}
|
||||
|
||||
function addCipherPrototypeFunctions(constructor) {
|
||||
@@ -271,7 +273,7 @@ function Decipher(cipher, password, options) {
|
||||
if (!(this instanceof Decipher))
|
||||
return new Decipher(cipher, password, options);
|
||||
|
||||
createCipher.call(this, cipher, password, options, false);
|
||||
ReflectApply(createCipher, this, [cipher, password, options, false]);
|
||||
}
|
||||
|
||||
ObjectSetPrototypeOf(Decipher.prototype, LazyTransform.prototype);
|
||||
@@ -287,7 +289,7 @@ function Decipheriv(cipher, key, iv, options) {
|
||||
if (!(this instanceof Decipheriv))
|
||||
return new Decipheriv(cipher, key, iv, options);
|
||||
|
||||
createCipherWithIV.call(this, cipher, key, options, false, iv);
|
||||
ReflectApply(createCipherWithIV, this, [cipher, key, options, false, iv]);
|
||||
}
|
||||
|
||||
ObjectSetPrototypeOf(Decipheriv.prototype, LazyTransform.prototype);
|
||||
@@ -315,8 +317,8 @@ function getCipherInfo(nameOrNid, options) {
|
||||
|
||||
const ret = _getCipherInfo({}, nameOrNid, keyLength, ivLength);
|
||||
if (ret !== undefined) {
|
||||
if (ret.name) ret.name = ret.name.toLowerCase();
|
||||
if (ret.type) ret.type = ret.type.toLowerCase();
|
||||
if (ret.name) ret.name = StringPrototypeToLowerCase(ret.name);
|
||||
if (ret.type) ret.type = StringPrototypeToLowerCase(ret.type);
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
'use strict';
|
||||
|
||||
const {
|
||||
ArrayBufferPrototypeSlice,
|
||||
FunctionPrototypeCall,
|
||||
MathFloor,
|
||||
ObjectDefineProperty,
|
||||
@@ -475,7 +476,9 @@ async function asyncDeriveBitsECDH(algorithm, baseKey, length) {
|
||||
if (byteLength < length)
|
||||
throw lazyDOMException('derived bit length is too small', 'OperationError');
|
||||
|
||||
return length === byteLength ? bits : bits.slice(0, length);
|
||||
return length === byteLength ?
|
||||
bits :
|
||||
ArrayBufferPrototypeSlice(bits, 0, length);
|
||||
}
|
||||
|
||||
async function asyncDeriveBitsDH(algorithm, baseKey, length) {
|
||||
@@ -528,7 +531,9 @@ async function asyncDeriveBitsDH(algorithm, baseKey, length) {
|
||||
if (byteLength < length)
|
||||
throw lazyDOMException('derived bit length is too small', 'OperationError');
|
||||
|
||||
return length === byteLength ? bits : bits.slice(0, length);
|
||||
return length === byteLength ?
|
||||
bits :
|
||||
ArrayBufferPrototypeSlice(bits, 0, length);
|
||||
}
|
||||
|
||||
function dhExportKey(key, format) {
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
const {
|
||||
ObjectSetPrototypeOf,
|
||||
ReflectApply,
|
||||
Symbol,
|
||||
} = primordials;
|
||||
|
||||
@@ -67,7 +68,7 @@ function Hash(algorithm, options) {
|
||||
this[kState] = {
|
||||
[kFinalized]: false
|
||||
};
|
||||
LazyTransform.call(this, options);
|
||||
ReflectApply(LazyTransform, this, [options]);
|
||||
}
|
||||
|
||||
ObjectSetPrototypeOf(Hash.prototype, LazyTransform.prototype);
|
||||
@@ -134,7 +135,7 @@ function Hmac(hmac, key, options) {
|
||||
this[kState] = {
|
||||
[kFinalized]: false
|
||||
};
|
||||
LazyTransform.call(this, options);
|
||||
ReflectApply(LazyTransform, this, [options]);
|
||||
}
|
||||
|
||||
ObjectSetPrototypeOf(Hmac.prototype, LazyTransform.prototype);
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
const {
|
||||
ObjectSetPrototypeOf,
|
||||
ReflectApply,
|
||||
} = primordials;
|
||||
|
||||
const {
|
||||
@@ -50,7 +51,7 @@ function Sign(algorithm, options) {
|
||||
this[kHandle] = new _Sign();
|
||||
this[kHandle].init(algorithm);
|
||||
|
||||
Writable.call(this, options);
|
||||
ReflectApply(Writable, this, [options]);
|
||||
}
|
||||
|
||||
ObjectSetPrototypeOf(Sign.prototype, Writable.prototype);
|
||||
@@ -164,7 +165,7 @@ function Verify(algorithm, options) {
|
||||
this[kHandle] = new _Verify();
|
||||
this[kHandle].init(algorithm);
|
||||
|
||||
Writable.call(this, options);
|
||||
ReflectApply(Writable, this, [options]);
|
||||
}
|
||||
|
||||
ObjectSetPrototypeOf(Verify.prototype, Writable.prototype);
|
||||
|
||||
@@ -7,6 +7,7 @@ const {
|
||||
ObjectDefineProperties,
|
||||
SafeSet,
|
||||
SymbolToStringTag,
|
||||
StringPrototypeRepeat,
|
||||
} = primordials;
|
||||
|
||||
const {
|
||||
@@ -494,7 +495,7 @@ async function wrapKey(format, key, wrappingKey, algorithm) {
|
||||
throw lazyDOMException('Invalid exported JWK key', 'DataError');
|
||||
const ec = new TextEncoder();
|
||||
const raw = JSONStringify(keyData);
|
||||
keyData = ec.encode(raw + ' '.repeat(8 - (raw.length % 8)));
|
||||
keyData = ec.encode(raw + StringPrototypeRepeat(' ', 8 - (raw.length % 8)));
|
||||
}
|
||||
|
||||
return cipherOrWrap(
|
||||
|
||||
Reference in new issue
Block a user