Skip to content

Commit e9b22e9

Browse files
committed
crypto: add better scrypt option aliases
Make parameter names available in a human-readable way, for more accessible/self-documenting usage of the `scrypt` functions. This implements a review comment from the original PR that has not been addressed. Refs: #20816 (comment) PR-URL: #21525 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Tobias Nießen <[email protected]> Reviewed-By: Tiancheng "Timothy" Gu <[email protected]>
1 parent 07c514c commit e9b22e9

File tree

3 files changed

+79
-14
lines changed

3 files changed

+79
-14
lines changed

doc/api/crypto.md

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2137,15 +2137,23 @@ request.
21372137
### crypto.scrypt(password, salt, keylen[, options], callback)
21382138
<!-- YAML
21392139
added: v10.5.0
2140+
changes:
2141+
- version: REPLACEME
2142+
pr-url: https://github.com/nodejs/node/pull/XXX
2143+
description: The `cost`, `blockSize` and `parallelization` option names
2144+
have been added.
21402145
-->
21412146
* `password` {string|Buffer|TypedArray|DataView}
21422147
* `salt` {string|Buffer|TypedArray|DataView}
21432148
* `keylen` {number}
21442149
* `options` {Object}
2145-
- `N` {number} CPU/memory cost parameter. Must be a power of two greater
2150+
- `cost` {number} CPU/memory cost parameter. Must be a power of two greater
21462151
than one. **Default:** `16384`.
2147-
- `r` {number} Block size parameter. **Default:** `8`.
2148-
- `p` {number} Parallelization parameter. **Default:** `1`.
2152+
- `blockSize` {number} Block size parameter. **Default:** `8`.
2153+
- `parallelization` {number} Parallelization parameter. **Default:** `1`.
2154+
- `N` {number} Alias for `cost`. Only one of both may be specified.
2155+
- `r` {number} Alias for `blockSize`. Only one of both may be specified.
2156+
- `p` {number} Alias for `parallelization`. Only one of both may be specified.
21492157
- `maxmem` {number} Memory upper bound. It is an error when (approximately)
21502158
`128 * N * r > maxmem`. **Default:** `32 * 1024 * 1024`.
21512159
* `callback` {Function}
@@ -2183,15 +2191,23 @@ crypto.scrypt('secret', 'salt', 64, { N: 1024 }, (err, derivedKey) => {
21832191
### crypto.scryptSync(password, salt, keylen[, options])
21842192
<!-- YAML
21852193
added: v10.5.0
2194+
changes:
2195+
- version: REPLACEME
2196+
pr-url: https://github.com/nodejs/node/pull/XXX
2197+
description: The `cost`, `blockSize` and `parallelization` option names
2198+
have been added.
21862199
-->
21872200
* `password` {string|Buffer|TypedArray|DataView}
21882201
* `salt` {string|Buffer|TypedArray|DataView}
21892202
* `keylen` {number}
21902203
* `options` {Object}
2191-
- `N` {number} CPU/memory cost parameter. Must be a power of two greater
2204+
- `cost` {number} CPU/memory cost parameter. Must be a power of two greater
21922205
than one. **Default:** `16384`.
2193-
- `r` {number} Block size parameter. **Default:** `8`.
2194-
- `p` {number} Parallelization parameter. **Default:** `1`.
2206+
- `blockSize` {number} Block size parameter. **Default:** `8`.
2207+
- `parallelization` {number} Parallelization parameter. **Default:** `1`.
2208+
- `N` {number} Alias for `cost`. Only one of both may be specified.
2209+
- `r` {number} Alias for `blockSize`. Only one of both may be specified.
2210+
- `p` {number} Alias for `parallelization`. Only one of both may be specified.
21952211
- `maxmem` {number} Memory upper bound. It is an error when (approximately)
21962212
`128 * N * r > maxmem`. **Default:** `32 * 1024 * 1024`.
21972213
* Returns: {Buffer}

lib/internal/crypto/scrypt.js

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,13 +80,26 @@ function check(password, salt, keylen, options, callback) {
8080

8181
let { N, r, p, maxmem } = defaults;
8282
if (options && options !== defaults) {
83-
if (options.hasOwnProperty('N'))
83+
let has_N, has_r, has_p;
84+
if (has_N = (options.N !== undefined))
8485
N = validateInt32(options.N, 'N', 0, INT_MAX);
85-
if (options.hasOwnProperty('r'))
86+
if (options.cost !== undefined) {
87+
if (has_N) throw new ERR_CRYPTO_SCRYPT_INVALID_PARAMETER();
88+
N = validateInt32(options.cost, 'cost', 0, INT_MAX);
89+
}
90+
if (has_r = (options.r !== undefined))
8691
r = validateInt32(options.r, 'r', 0, INT_MAX);
87-
if (options.hasOwnProperty('p'))
92+
if (options.blockSize !== undefined) {
93+
if (has_r) throw new ERR_CRYPTO_SCRYPT_INVALID_PARAMETER();
94+
r = validateInt32(options.blockSize, 'blockSize', 0, INT_MAX);
95+
}
96+
if (has_p = (options.p !== undefined))
8897
p = validateInt32(options.p, 'p', 0, INT_MAX);
89-
if (options.hasOwnProperty('maxmem'))
98+
if (options.parallelization !== undefined) {
99+
if (has_p) throw new ERR_CRYPTO_SCRYPT_INVALID_PARAMETER();
100+
p = validateInt32(options.parallelization, 'parallelization', 0, INT_MAX);
101+
}
102+
if (options.maxmem !== undefined)
90103
maxmem = validateInt32(options.maxmem, 'maxmem', 0, INT_MAX);
91104
if (N === 0) N = defaults.N;
92105
if (r === 0) r = defaults.r;

test/parallel/test-crypto-scrypt.js

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,14 +56,50 @@ const good = [
5656
'7023bdcb3afd7348461c06cd81fd38ebfda8fbba904f8e3ea9b543f6545da1f2' +
5757
'd5432955613f0fcf62d49705242a9af9e61e85dc0d651e40dfcf017b45575887',
5858
},
59+
{
60+
pass: '',
61+
salt: '',
62+
keylen: 64,
63+
cost: 16,
64+
parallelization: 1,
65+
blockSize: 1,
66+
expected:
67+
'77d6576238657b203b19ca42c18a0497f16b4844e3074ae8dfdffa3fede21442' +
68+
'fcd0069ded0948f8326a753a0fc81f17e8d3e0fb2e0d3628cf35e20c38d18906',
69+
},
70+
{
71+
pass: 'password',
72+
salt: 'NaCl',
73+
keylen: 64,
74+
cost: 1024,
75+
parallelization: 16,
76+
blockSize: 8,
77+
expected:
78+
'fdbabe1c9d3472007856e7190d01e9fe7c6ad7cbc8237830e77376634b373162' +
79+
'2eaf30d92e22a3886ff109279d9830dac727afb94a83ee6d8360cbdfa2cc0640',
80+
},
81+
{
82+
pass: 'pleaseletmein',
83+
salt: 'SodiumChloride',
84+
keylen: 64,
85+
cost: 16384,
86+
parallelization: 1,
87+
blockSize: 8,
88+
expected:
89+
'7023bdcb3afd7348461c06cd81fd38ebfda8fbba904f8e3ea9b543f6545da1f2' +
90+
'd5432955613f0fcf62d49705242a9af9e61e85dc0d651e40dfcf017b45575887',
91+
},
5992
];
6093

6194
// Test vectors that should fail.
6295
const bad = [
63-
{ N: 1, p: 1, r: 1 }, // N < 2
64-
{ N: 3, p: 1, r: 1 }, // Not power of 2.
65-
{ N: 2 ** 16, p: 1, r: 1 }, // N >= 2**(r*16)
66-
{ N: 2, p: 2 ** 30, r: 1 }, // p > (2**30-1)/r
96+
{ N: 1, p: 1, r: 1 }, // N < 2
97+
{ N: 3, p: 1, r: 1 }, // Not power of 2.
98+
{ N: 2 ** 16, p: 1, r: 1 }, // N >= 2**(r*16)
99+
{ N: 2, p: 2 ** 30, r: 1 }, // p > (2**30-1)/r
100+
{ N: 1, cost: 1 }, // both N and cost
101+
{ p: 1, parallelization: 1 }, // both p and parallelization
102+
{ r: 1, blockSize: 1 } // both r and blocksize
67103
];
68104

69105
// Test vectors where 128*N*r exceeds maxmem.

0 commit comments

Comments
 (0)