MCPcopy Index your code
hub / github.com/nodejs/node / normalizeAlgorithm

Function normalizeAlgorithm

lib/internal/crypto/util.js:606–676  ·  view source on GitHub ↗
(algorithm, op)

Source from the content-addressed store, hash-verified

604// adapted for Node.js from Deno's implementation
605// https://github.com/denoland/deno/blob/v1.29.1/ext/crypto/00_crypto.js#L195
606function normalizeAlgorithm(algorithm, op) {
607 if (typeof algorithm === 'string')
608 return normalizeAlgorithm({ name: algorithm }, op);
609
610 webidl ??= require('internal/crypto/webidl');
611
612 // 1.
613 const registeredAlgorithms = kSupportedAlgorithms[op];
614 // 2. 3.
615 const initialAlg = webidl.converters.Algorithm(algorithm,
616 kNormalizeAlgorithmOpts);
617 // 4.
618 let algName = initialAlg.name;
619
620 // 5. Case-insensitive lookup via pre-built Map (O(1) instead of O(n)).
621 const canonicalName = kAlgorithmNameMap[op]?.get(
622 StringPrototypeToUpperCase(algName));
623 if (canonicalName === undefined)
624 throw lazyDOMException('Unrecognized algorithm name', 'NotSupportedError');
625
626 algName = canonicalName;
627 const desiredType = registeredAlgorithms[algName];
628
629 // Fast path everything below if the registered dictionary is null
630 if (desiredType === null)
631 return { name: algName };
632
633 // 6.
634 const normalizedAlgorithm = webidl.converters[desiredType](
635 { __proto__: algorithm, name: algName },
636 kNormalizeAlgorithmOpts,
637 );
638 // 7.
639 normalizedAlgorithm.name = algName;
640
641 // 9. 10. Pre-computed keys and types from simpleAlgorithmDictionaries.
642 const dictMeta = simpleAlgorithmDictionaries[desiredType];
643 if (dictMeta) {
644 const { keys: dictKeys, types: dictTypes } = dictMeta;
645 for (let i = 0; i < dictKeys.length; i++) {
646 const member = dictKeys[i];
647 const idlType = dictTypes[member];
648 const idlValue = normalizedAlgorithm[member];
649 // 3.
650 if (idlType === 'BufferSource' && idlValue) {
651 const isView = ArrayBufferIsView(idlValue);
652 const idlValueBytes = isView ?
653 new Uint8Array(
654 getDataViewOrTypedArrayBuffer(idlValue),
655 getDataViewOrTypedArrayByteOffset(idlValue),
656 getDataViewOrTypedArrayByteLength(idlValue),
657 ) :
658 new Uint8Array(
659 idlValue,
660 0,
661 ArrayBufferPrototypeGetByteLength(idlValue),
662 );
663 normalizedAlgorithm[member] = TypedArrayPrototypeSlice(

Callers 15

digestImplFunction · 0.70
generateKeyImplFunction · 0.70
deriveBitsImplFunction · 0.70
deriveKeyImplFunction · 0.70
exportKeySyncFunction · 0.70
importKeyImplFunction · 0.70
wrapKeyImplFunction · 0.70
unwrapKeyImplFunction · 0.70
signVerifyFunction · 0.70
encryptImplFunction · 0.70
decryptImplFunction · 0.70
encapsulateBitsImplFunction · 0.70

Calls 6

lazyDOMExceptionFunction · 0.85
getMethod · 0.65
requireFunction · 0.50

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…