Skip to content

Commit 92ef2ad

Browse files
panvaaduh95
authored andcommitted
lib: prefer primordials in SubtleCrypto
PR-URL: #62226 Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Jordan Harband <ljharb@gmail.com> Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com> Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com> Reviewed-By: Zeyu "Alex" Yang <himself65@outlook.com>
1 parent 8513893 commit 92ef2ad

File tree

5 files changed

+23
-18
lines changed

5 files changed

+23
-18
lines changed

lib/internal/crypto/cfrg.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
const {
44
SafeSet,
5+
StringPrototypeToLowerCase,
56
} = primordials;
67

78
const { Buffer } = require('buffer');
@@ -332,7 +333,7 @@ function cfrgImportKey(
332333
return undefined;
333334
}
334335

335-
if (keyObject.asymmetricKeyType !== name.toLowerCase()) {
336+
if (keyObject.asymmetricKeyType !== StringPrototypeToLowerCase(name)) {
336337
throw lazyDOMException('Invalid key type', 'DataError');
337338
}
338339

lib/internal/crypto/ml_dsa.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
const {
44
SafeSet,
5+
StringPrototypeToLowerCase,
56
TypedArrayPrototypeGetBuffer,
67
TypedArrayPrototypeSet,
78
Uint8Array,
@@ -276,7 +277,7 @@ function mlDsaImportKey(
276277
return undefined;
277278
}
278279

279-
if (keyObject.asymmetricKeyType !== name.toLowerCase()) {
280+
if (keyObject.asymmetricKeyType !== StringPrototypeToLowerCase(name)) {
280281
throw lazyDOMException('Invalid key type', 'DataError');
281282
}
282283

lib/internal/crypto/ml_kem.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
const {
44
PromiseWithResolvers,
55
SafeSet,
6+
StringPrototypeToLowerCase,
67
TypedArrayPrototypeGetBuffer,
78
TypedArrayPrototypeSet,
89
Uint8Array,
@@ -209,7 +210,7 @@ function mlKemImportKey(
209210
return undefined;
210211
}
211212

212-
if (keyObject.asymmetricKeyType !== name.toLowerCase()) {
213+
if (keyObject.asymmetricKeyType !== StringPrototypeToLowerCase(name)) {
213214
throw lazyDOMException('Invalid key type', 'DataError');
214215
}
215216

lib/internal/crypto/util.js

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ const {
1515
ObjectEntries,
1616
ObjectKeys,
1717
ObjectPrototypeHasOwnProperty,
18-
Promise,
18+
PromiseWithResolvers,
1919
StringPrototypeToUpperCase,
2020
Symbol,
2121
TypedArrayPrototypeGetBuffer,
@@ -656,15 +656,15 @@ function onDone(resolve, reject, err, result) {
656656
}
657657

658658
function jobPromise(getJob) {
659-
return new Promise((resolve, reject) => {
660-
try {
661-
const job = getJob();
662-
job.ondone = FunctionPrototypeBind(onDone, job, resolve, reject);
663-
job.run();
664-
} catch (err) {
665-
onDone(resolve, reject, err);
666-
}
667-
});
659+
const { promise, resolve, reject } = PromiseWithResolvers();
660+
try {
661+
const job = getJob();
662+
job.ondone = FunctionPrototypeBind(onDone, job, resolve, reject);
663+
job.run();
664+
} catch (err) {
665+
onDone(resolve, reject, err);
666+
}
667+
return promise;
668668
}
669669

670670
// In WebCrypto, the publicExponent option in RSA is represented as a

lib/internal/crypto/webidl.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -195,12 +195,14 @@ converters.object = (V, opts) => {
195195

196196
const isNonSharedArrayBuffer = isArrayBuffer;
197197

198+
/**
199+
* @param {string | object} V - The hash algorithm identifier (string or object).
200+
* @param {string} label - The dictionary name for the error message.
201+
*/
198202
function ensureSHA(V, label) {
199-
if (
200-
typeof V === 'string' ?
201-
!StringPrototypeStartsWith(StringPrototypeToLowerCase(V), 'sha') :
202-
V.name?.toLowerCase?.().startsWith('sha') === false
203-
)
203+
const name = typeof V === 'string' ? V : V.name;
204+
if (typeof name !== 'string' ||
205+
!StringPrototypeStartsWith(StringPrototypeToLowerCase(name), 'sha'))
204206
throw lazyDOMException(
205207
`Only SHA hashes are supported in ${label}`, 'NotSupportedError');
206208
}

0 commit comments

Comments
 (0)