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

Function toCryptoKey

lib/internal/crypto/keys.js:764–824  ·  view source on GitHub ↗

* Converts an asymmetric KeyObjectHandle to a CryptoKey by dispatching to * the algorithm-specific Web Crypto import path. This preserves the same * algorithm and usage validation used by SubtleCrypto.importKey(). * @param {KeyObjectHandle} keyData * @param {object} algorithm * @param {boolean}

(
  keyData,
  algorithm,
  extractable,
  keyUsages,
)

Source from the content-addressed store, hash-verified

762 * @returns {CryptoKey}
763 */
764function toCryptoKey(
765 keyData,
766 algorithm,
767 extractable,
768 keyUsages,
769) {
770 let result;
771 switch (algorithm.name) {
772 case 'RSASSA-PKCS1-v1_5':
773 // Fall through
774 case 'RSA-PSS':
775 // Fall through
776 case 'RSA-OAEP':
777 result = require('internal/crypto/rsa')
778 .rsaImportKey('KeyObjectHandle', keyData, algorithm, extractable, keyUsages);
779 break;
780 case 'ECDSA':
781 // Fall through
782 case 'ECDH':
783 result = require('internal/crypto/ec')
784 .ecImportKey('KeyObjectHandle', keyData, algorithm, extractable, keyUsages);
785 break;
786 case 'Ed25519':
787 // Fall through
788 case 'Ed448':
789 // Fall through
790 case 'X25519':
791 // Fall through
792 case 'X448':
793 result = require('internal/crypto/cfrg')
794 .cfrgImportKey('KeyObjectHandle', keyData, algorithm, extractable, keyUsages);
795 break;
796 case 'ML-DSA-44':
797 // Fall through
798 case 'ML-DSA-65':
799 // Fall through
800 case 'ML-DSA-87':
801 result = require('internal/crypto/ml_dsa')
802 .mlDsaImportKey('KeyObjectHandle', keyData, algorithm, extractable, keyUsages);
803 break;
804 case 'ML-KEM-512':
805 // Fall through
806 case 'ML-KEM-768':
807 // Fall through
808 case 'ML-KEM-1024':
809 result = require('internal/crypto/ml_kem')
810 .mlKemImportKey('KeyObjectHandle', keyData, algorithm, extractable, keyUsages);
811 break;
812 default:
813 throw lazyDOMException('Unrecognized algorithm name', 'NotSupportedError');
814 }
815
816 const resultType = getCryptoKeyType(result);
817 if (resultType === 'private' && getCryptoKeyUsagesMask(result) === 0) {
818 throw lazyDOMException(
819 `Usages cannot be empty when importing a ${resultType} key.`,
820 'SyntaxError');
821 }

Callers 2

toCryptoKeyMethod · 0.85
toPublicCryptoKeyFunction · 0.85

Calls 4

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

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…