Skip to content

Commit d005ae0

Browse files
addaleaxtargos
authored andcommitted
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 c85d00b commit d005ae0

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
@@ -2152,15 +2152,23 @@ request.
21522152
### crypto.scrypt(password, salt, keylen[, options], callback)
21532153
<!-- YAML
21542154
added: v10.5.0
2155+
changes:
2156+
- version: REPLACEME
2157+
pr-url: https://github.com/nodejs/node/pull/XXX
2158+
description: The `cost`, `blockSize` and `parallelization` option names
2159+
have been added.
21552160
-->
21562161
*`password`{string|Buffer|TypedArray|DataView}
21572162
*`salt`{string|Buffer|TypedArray|DataView}
21582163
*`keylen`{number}
21592164
*`options`{Object}
2160-
-`N`{number} CPU/memory cost parameter. Must be a power of two greater
2165+
-`cost`{number} CPU/memory cost parameter. Must be a power of two greater
21612166
than one. **Default:**`16384`.
2162-
-`r`{number} Block size parameter. **Default:**`8`.
2163-
-`p`{number} Parallelization parameter. **Default:**`1`.
2167+
-`blockSize`{number} Block size parameter. **Default:**`8`.
2168+
-`parallelization`{number} Parallelization parameter. **Default:**`1`.
2169+
-`N`{number} Alias for `cost`. Only one of both may be specified.
2170+
-`r`{number} Alias for `blockSize`. Only one of both may be specified.
2171+
-`p`{number} Alias for `parallelization`. Only one of both may be specified.
21642172
-`maxmem`{number} Memory upper bound. It is an error when (approximately)
21652173
`128 * N * r > maxmem`. **Default:**`32 * 1024 * 1024`.
21662174
*`callback`{Function}
@@ -2198,15 +2206,23 @@ crypto.scrypt('secret', 'salt', 64,{N: 1024 }, (err, derivedKey) =>{
21982206
### crypto.scryptSync(password, salt, keylen[, options])
21992207
<!-- YAML
22002208
added: v10.5.0
2209+
changes:
2210+
- version: REPLACEME
2211+
pr-url: https://github.com/nodejs/node/pull/XXX
2212+
description: The `cost`, `blockSize` and `parallelization` option names
2213+
have been added.
22012214
-->
22022215
*`password`{string|Buffer|TypedArray|DataView}
22032216
*`salt`{string|Buffer|TypedArray|DataView}
22042217
*`keylen`{number}
22052218
*`options`{Object}
2206-
-`N`{number} CPU/memory cost parameter. Must be a power of two greater
2219+
-`cost`{number} CPU/memory cost parameter. Must be a power of two greater
22072220
than one. **Default:**`16384`.
2208-
-`r`{number} Block size parameter. **Default:**`8`.
2209-
-`p`{number} Parallelization parameter. **Default:**`1`.
2221+
-`blockSize`{number} Block size parameter. **Default:**`8`.
2222+
-`parallelization`{number} Parallelization parameter. **Default:**`1`.
2223+
-`N`{number} Alias for `cost`. Only one of both may be specified.
2224+
-`r`{number} Alias for `blockSize`. Only one of both may be specified.
2225+
-`p`{number} Alias for `parallelization`. Only one of both may be specified.
22102226
-`maxmem`{number} Memory upper bound. It is an error when (approximately)
22112227
`128 * N * r > maxmem`. **Default:**`32 * 1024 * 1024`.
22122228
* 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+
lethas_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)thrownewERR_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)thrownewERR_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)thrownewERR_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
constbad=[
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)