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

Function parseKeyEncoding

lib/internal/crypto/keys.js:449–498  ·  view source on GitHub ↗
(enc, keyType, isPublic, objName)

Source from the content-addressed store, hash-verified

447}
448
449function parseKeyEncoding(enc, keyType, isPublic, objName) {
450 validateObject(enc, 'options');
451
452 const isInput = keyType === undefined;
453
454 const {
455 format,
456 type,
457 } = parseKeyFormatAndType(enc, keyType, isPublic, objName);
458
459 let cipher, passphrase, encoding;
460 if (isPublic !== true) {
461 ({ cipher, passphrase, encoding } = enc);
462
463 if (format === kKeyFormatRawPrivate || format === kKeyFormatRawSeed) {
464 if (cipher != null || passphrase !== undefined) {
465 throw new ERR_CRYPTO_INCOMPATIBLE_KEY_OPTIONS(
466 'raw format', 'does not support encryption');
467 }
468 return { format, type };
469 }
470
471 if (!isInput) {
472 if (cipher != null) {
473 if (typeof cipher !== 'string')
474 throw new ERR_INVALID_ARG_VALUE(option('cipher', objName), cipher);
475 if (format === kKeyFormatDER &&
476 (type === kKeyEncodingPKCS1 ||
477 type === kKeyEncodingSEC1)) {
478 throw new ERR_CRYPTO_INCOMPATIBLE_KEY_OPTIONS(
479 encodingNames[type], 'does not support encryption');
480 }
481 } else if (passphrase !== undefined) {
482 throw new ERR_INVALID_ARG_VALUE(option('cipher', objName), cipher);
483 }
484 }
485
486 if ((isInput && passphrase !== undefined &&
487 !isStringOrBuffer(passphrase)) ||
488 (!isInput && cipher != null && !isStringOrBuffer(passphrase))) {
489 throw new ERR_INVALID_ARG_VALUE(option('passphrase', objName),
490 passphrase);
491 }
492 }
493
494 if (passphrase !== undefined)
495 passphrase = getArrayBufferOrView(passphrase, 'key.passphrase', encoding);
496
497 return { format, type, cipher, passphrase };
498}
499
500// Parses the public key encoding based on an object. keyType must be undefined
501// when this is used to parse an input encoding and must be a valid key type if

Callers 3

parsePublicKeyEncodingFunction · 0.70
parsePrivateKeyEncodingFunction · 0.70
prepareAsymmetricKeyFunction · 0.70

Calls 3

parseKeyFormatAndTypeFunction · 0.85
optionFunction · 0.85
isStringOrBufferFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…