GeezerVison is showing github.com — crypto: add better scrypt option aliases · nodejs/node@d005ae0
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: doc/api/crypto.md +22-6Lines changed: 22 additions & 6 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
@@ -2152,15 +2152,23 @@ request. | 2152 | 2152 | ### crypto.scrypt(password, salt, keylen[, options], callback) | 2153 | 2153 | <!-- YAML | 2154 | 2154 | 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. | 2155 | 2160 | --> | 2156 | 2161 | * `password` {string|Buffer|TypedArray|DataView} | 2157 | 2162 | * `salt` {string|Buffer|TypedArray|DataView} | 2158 | 2163 | * `keylen` {number} | 2159 | 2164 | * `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 | 2161 | 2166 | 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. | 2164 | 2172 | - `maxmem` {number} Memory upper bound. It is an error when (approximately) | 2165 | 2173 | `128 * N * r > maxmem`. **Default:** `32 * 1024 * 1024`. | 2166 | 2174 | * `callback` {Function} | @@ -2198,15 +2206,23 @@ crypto.scrypt('secret', 'salt', 64, { N: 1024 }, (err, derivedKey) => { | 2198 | 2206 | ### crypto.scryptSync(password, salt, keylen[, options]) | 2199 | 2207 | <!-- YAML | 2200 | 2208 | 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. | 2201 | 2214 | --> | 2202 | 2215 | * `password` {string|Buffer|TypedArray|DataView} | 2203 | 2216 | * `salt` {string|Buffer|TypedArray|DataView} | 2204 | 2217 | * `keylen` {number} | 2205 | 2218 | * `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 | 2207 | 2220 | 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. | 2210 | 2226 | - `maxmem` {number} Memory upper bound. It is an error when (approximately) | 2211 | 2227 | `128 * N * r > maxmem`. **Default:** `32 * 1024 * 1024`. | 2212 | 2228 | * Returns: {Buffer} | |
Collapse file Copy file name to clipboardExpand all lines: lib/internal/crypto/scrypt.js +17-4Lines changed: 17 additions & 4 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -80,13 +80,26 @@ function check(password, salt, keylen, options, callback) { | 80 | 80 |
| 81 | 81 | let { N, r, p, maxmem } = defaults; | 82 | 82 | if (options && options !== defaults) { | 83 | | - if (options.hasOwnProperty('N')) | | 83 | + let has_N, has_r, has_p; | | 84 | + if (has_N = (options.N !== undefined)) | 84 | 85 | 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)) | 86 | 91 | 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)) | 88 | 97 | 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) | 90 | 103 | maxmem = validateInt32(options.maxmem, 'maxmem', 0, INT_MAX); | 91 | 104 | if (N === 0) N = defaults.N; | 92 | 105 | if (r === 0) r = defaults.r; | |
Collapse file Copy file name to clipboardExpand all lines: test/parallel/test-crypto-scrypt.js +40-4Lines changed: 40 additions & 4 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -56,14 +56,50 @@ const good = [ | 56 | 56 | '7023bdcb3afd7348461c06cd81fd38ebfda8fbba904f8e3ea9b543f6545da1f2' + | 57 | 57 | 'd5432955613f0fcf62d49705242a9af9e61e85dc0d651e40dfcf017b45575887', | 58 | 58 | }, | | 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 | + }, | 59 | 92 | ]; | 60 | 93 |
| 61 | 94 | // Test vectors that should fail. | 62 | 95 | 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 | 67 | 103 | ]; | 68 | 104 |
| 69 | 105 | // Test vectors where 128*N*r exceeds maxmem. | |
|