crypto: align key argument names in docs and error messages · nodejs/node@bb2857b · GitHub
Skip to content

Commit bb2857b

Browse files
panvasxa
authored andcommitted
crypto: align key argument names in docs and error messages
Signed-off-by: Filip Skokan <panva.ip@gmail.com> PR-URL: #62527 Backport-PR-URL: #63563 Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent b9d5e87 commit bb2857b

7 files changed

Lines changed: 158 additions & 16 deletions

File tree

doc/api/crypto.md

Lines changed: 7 additions & 7 deletions

doc/api/deprecations.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4484,7 +4484,7 @@ will throw an error in a future version.
44844484
[`Sign.prototype.sign()`]: crypto.md#signsignprivatekey-outputencoding
44854485
[`SlowBuffer`]: buffer.md#class-slowbuffer
44864486
[`String.prototype.toWellFormed`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/toWellFormed
4487-
[`Verify.prototype.verify()`]: crypto.md#verifyverifyobject-signature-signatureencoding
4487+
[`Verify.prototype.verify()`]: crypto.md#verifyverifykey-signature-signatureencoding
44884488
[`WriteStream.open()`]: fs.md#class-fswritestream
44894489
[`assert.CallTracker`]: assert.md#class-assertcalltracker
44904490
[`assert`]: assert.md

lib/internal/crypto/cipher.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,14 +63,15 @@ const { normalizeEncoding } = require('internal/util');
6363
const { StringDecoder } = require('string_decoder');
6464

6565
function rsaFunctionFor(method, defaultPadding, keyType) {
66-
return (options, buffer) => {
66+
const keyName = keyType === 'private' ? 'privateKey' : undefined;
67+
return (key, buffer) => {
6768
const { format, type, data, passphrase, namedCurve } =
6869
keyType === 'private' ?
69-
preparePrivateKey(options) :
70-
preparePublicOrPrivateKey(options);
71-
const padding = options.padding || defaultPadding;
72-
const { oaepHash, encoding } = options;
73-
let { oaepLabel } = options;
70+
preparePrivateKey(key, keyName) :
71+
preparePublicOrPrivateKey(key, keyName);
72+
const padding = key.padding || defaultPadding;
73+
const { oaepHash, encoding } = key;
74+
let { oaepLabel } = key;
7475
if (oaepHash !== undefined)
7576
validateString(oaepHash, 'key.oaepHash');
7677
if (oaepLabel !== undefined)

lib/internal/crypto/keygen.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ function parseKeyEncoding(keyType, options = kEmptyObject) {
148148
format: publicFormat,
149149
type: publicType,
150150
} = parsePublicKeyEncoding(publicKeyEncoding, keyType,
151-
'publicKeyEncoding'));
151+
'options.publicKeyEncoding'));
152152
} else {
153153
throw new ERR_INVALID_ARG_VALUE('options.publicKeyEncoding',
154154
publicKeyEncoding);
@@ -164,7 +164,7 @@ function parseKeyEncoding(keyType, options = kEmptyObject) {
164164
cipher,
165165
passphrase,
166166
} = parsePrivateKeyEncoding(privateKeyEncoding, keyType,
167-
'privateKeyEncoding'));
167+
'options.privateKeyEncoding'));
168168
} else {
169169
throw new ERR_INVALID_ARG_VALUE('options.privateKeyEncoding',
170170
privateKeyEncoding);

test/parallel/test-crypto-dh-stateless.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -398,6 +398,49 @@ for (const { privateKey: alicePriv, publicKey: bobPub } of [
398398
}
399399
}
400400

401+
// Test that error messages include the correct property path
402+
{
403+
const kp = crypto.generateKeyPairSync('x25519');
404+
const pub = kp.publicKey.export({ type: 'spki', format: 'pem' });
405+
const priv = kp.privateKey.export({ type: 'pkcs8', format: 'pem' });
406+
407+
// Invalid privateKey format
408+
assert.throws(() => crypto.diffieHellman({
409+
privateKey: { key: Buffer.alloc(0), format: 'banana', type: 'pkcs8' },
410+
publicKey: pub,
411+
}), {
412+
code: 'ERR_INVALID_ARG_VALUE',
413+
message: /options\.privateKey\.format/,
414+
});
415+
416+
// Invalid privateKey type
417+
assert.throws(() => crypto.diffieHellman({
418+
privateKey: { key: Buffer.alloc(0), format: 'der', type: 'banana' },
419+
publicKey: pub,
420+
}), {
421+
code: 'ERR_INVALID_ARG_VALUE',
422+
message: /options\.privateKey\.type/,
423+
});
424+
425+
// Invalid publicKey format
426+
assert.throws(() => crypto.diffieHellman({
427+
publicKey: { key: Buffer.alloc(0), format: 'banana', type: 'spki' },
428+
privateKey: priv,
429+
}), {
430+
code: 'ERR_INVALID_ARG_VALUE',
431+
message: /options\.publicKey\.format/,
432+
});
433+
434+
// Invalid publicKey type
435+
assert.throws(() => crypto.diffieHellman({
436+
publicKey: { key: Buffer.alloc(0), format: 'der', type: 'banana' },
437+
privateKey: priv,
438+
}), {
439+
code: 'ERR_INVALID_ARG_VALUE',
440+
message: /options\.publicKey\.type/,
441+
});
442+
}
443+
401444
// Test C++ error conditions
402445
{
403446
const ec256 = crypto.generateKeyPairSync('ec', { namedCurve: 'P-256' });

test/parallel/test-crypto-key-objects.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1085,3 +1085,38 @@ const privateDsa = fixtures.readKey('dsa_private_encrypted_1025.pem',
10851085
}, { code: 'ERR_INVALID_ARG_TYPE', message: /The "key\.key" property must be of type object/ });
10861086
}
10871087
}
1088+
1089+
// Test that createPublicKey/createPrivateKey error messages use 'key.<property>' paths
1090+
{
1091+
// createPrivateKey with invalid format
1092+
assert.throws(() => {
1093+
createPrivateKey({ key: Buffer.alloc(0), format: 'banana', type: 'pkcs8' });
1094+
}, {
1095+
code: 'ERR_INVALID_ARG_VALUE',
1096+
message: /key\.format/,
1097+
});
1098+
1099+
// createPrivateKey with invalid type
1100+
assert.throws(() => {
1101+
createPrivateKey({ key: Buffer.alloc(0), format: 'der', type: 'banana' });
1102+
}, {
1103+
code: 'ERR_INVALID_ARG_VALUE',
1104+
message: /key\.type/,
1105+
});
1106+
1107+
// createPublicKey with invalid format
1108+
assert.throws(() => {
1109+
createPublicKey({ key: Buffer.alloc(0), format: 'banana', type: 'spki' });
1110+
}, {
1111+
code: 'ERR_INVALID_ARG_VALUE',
1112+
message: /key\.format/,
1113+
});
1114+
1115+
// createPublicKey with invalid type
1116+
assert.throws(() => {
1117+
createPublicKey({ key: Buffer.alloc(0), format: 'der', type: 'banana' });
1118+
}, {
1119+
code: 'ERR_INVALID_ARG_VALUE',
1120+
message: /key\.type/,
1121+
});
1122+
}

test/parallel/test-crypto-sign-verify.js

Lines changed: 63 additions & 0 deletions

0 commit comments

Comments
 (0)