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

Function prepareAsymmetricKey

lib/internal/crypto/keys.js:552–631  ·  view source on GitHub ↗
(key, ctx, name = 'key')

Source from the content-addressed store, hash-verified

550
551
552function prepareAsymmetricKey(key, ctx, name = 'key') {
553 if (isKeyObject(key)) {
554 // Best case: A key object, as simple as that.
555 const type = getKeyObjectType(key);
556 validateAsymmetricKeyType(type, ctx, key);
557 return { data: getKeyObjectHandle(key) };
558 }
559 if (isCryptoKey(key)) {
560 emitDEP0203();
561 validateAsymmetricKeyType(getCryptoKeyType(key), ctx, key);
562 return { data: getCryptoKeyHandle(key) };
563 }
564 if (isStringOrBuffer(key)) {
565 // Expect PEM by default, mostly for backward compatibility.
566 return { format: kKeyFormatPEM, data: getArrayBufferOrView(key, name) };
567 }
568 if (typeof key === 'object') {
569 const { key: data, encoding, format } = key;
570
571 // The 'key' property can be a KeyObject as well to allow specifying
572 // additional options such as padding along with the key.
573 if (isKeyObject(data)) {
574 const type = getKeyObjectType(data);
575 validateAsymmetricKeyType(type, ctx, data);
576 return { data: getKeyObjectHandle(data) };
577 }
578 if (isCryptoKey(data)) {
579 emitDEP0203();
580 validateAsymmetricKeyType(getCryptoKeyType(data), ctx, data);
581 return { data: getCryptoKeyHandle(data) };
582 }
583 if (format === 'jwk') {
584 validateObject(data, `${name}.key`);
585 return { data, format: kKeyFormatJWK };
586 } else if (format === 'raw-public' || format === 'raw-private' ||
587 format === 'raw-seed') {
588 if ((ctx === kConsumePrivate || ctx === kCreatePrivate) &&
589 format === 'raw-public') {
590 throw new ERR_INVALID_ARG_VALUE(`${name}.format`, format);
591 }
592 if (!isArrayBufferView(data) && !isAnyArrayBuffer(data)) {
593 throw new ERR_INVALID_ARG_TYPE(
594 `${name}.key`,
595 ['ArrayBuffer', 'Buffer', 'TypedArray', 'DataView'],
596 data);
597 }
598 validateString(key.asymmetricKeyType, `${name}.asymmetricKeyType`);
599 if (key.asymmetricKeyType === 'ec') {
600 validateString(key.namedCurve, `${name}.namedCurve`);
601 }
602 const rawFormat = parseKeyFormat(format, undefined, `${name}.format`);
603 return {
604 data: getArrayBufferOrView(data, `${name}.key`),
605 format: rawFormat,
606 type: key.asymmetricKeyType,
607 namedCurve: key.namedCurve ?? null,
608 };
609 }

Callers 4

preparePrivateKeyFunction · 0.85
createPublicKeyFunction · 0.85
createPrivateKeyFunction · 0.85

Calls 11

isKeyObjectFunction · 0.85
getKeyObjectTypeFunction · 0.85
getKeyObjectHandleFunction · 0.85
isCryptoKeyFunction · 0.85
getCryptoKeyTypeFunction · 0.85
getCryptoKeyHandleFunction · 0.85
isStringOrBufferFunction · 0.85
parseKeyFormatFunction · 0.85
getKeyTypesFunction · 0.85
parseKeyEncodingFunction · 0.70

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…