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

Function toCryptoKeySecret

lib/internal/crypto/keys.js:693–752  ·  view source on GitHub ↗

* Converts a secret KeyObjectHandle to a CryptoKey by dispatching to the * algorithm-specific Web Crypto import path. * @param {KeyObjectHandle} keyData * @param {object} algorithm * @param {boolean} extractable * @param {string[]} keyUsages * @returns {CryptoKey}

(
  keyData,
  algorithm,
  extractable,
  keyUsages,
)

Source from the content-addressed store, hash-verified

691 * @returns {CryptoKey}
692 */
693function toCryptoKeySecret(
694 keyData,
695 algorithm,
696 extractable,
697 keyUsages,
698) {
699 let result;
700 switch (algorithm.name) {
701 case 'HMAC':
702 // Fall through
703 case 'KMAC128':
704 // Fall through
705 case 'KMAC256':
706 result = require('internal/crypto/mac')
707 .macImportKey('KeyObjectHandle', keyData, algorithm, extractable, keyUsages);
708 break;
709 case 'AES-CTR':
710 // Fall through
711 case 'AES-CBC':
712 // Fall through
713 case 'AES-GCM':
714 // Fall through
715 case 'AES-KW':
716 // Fall through
717 case 'AES-OCB':
718 result = require('internal/crypto/aes')
719 .aesImportKey(algorithm, 'KeyObjectHandle', keyData, extractable, keyUsages);
720 break;
721 case 'ChaCha20-Poly1305':
722 result = require('internal/crypto/chacha20_poly1305')
723 .c20pImportKey(algorithm, 'KeyObjectHandle', keyData, extractable, keyUsages);
724 break;
725 case 'HKDF':
726 // Fall through
727 case 'PBKDF2':
728 // Fall through
729 case 'Argon2d':
730 // Fall through
731 case 'Argon2i':
732 // Fall through
733 case 'Argon2id':
734 result = importGenericSecretKey(
735 algorithm,
736 'KeyObjectHandle',
737 keyData,
738 extractable,
739 keyUsages);
740 break;
741 default:
742 throw lazyDOMException('Unrecognized algorithm name', 'NotSupportedError');
743 }
744
745 if (getCryptoKeyUsagesMask(result) === 0) {
746 throw lazyDOMException(
747 `Usages cannot be empty when importing a ${getCryptoKeyType(result)} key.`,
748 'SyntaxError');
749 }
750

Callers 1

toCryptoKeyMethod · 0.85

Calls 5

importGenericSecretKeyFunction · 0.85
lazyDOMExceptionFunction · 0.85
getCryptoKeyUsagesMaskFunction · 0.85
getCryptoKeyTypeFunction · 0.85
requireFunction · 0.50

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…