Node.js Reference ยท crypto
Node.js crypto.scrypt() function
Reference for crypto.scrypt() and scryptSync() in Node.js: memory-hard password hashing, cost parameters N, r and p, salts, memory limits and safe verification.
crypto.scrypt(password, salt, keylen[, options], callback) derives a key of keylen bytes from a password using the scrypt algorithm. scrypt is deliberately slow and memory-hungry, which makes large-scale password guessing on GPUs and custom hardware expensive. crypto.scryptSync() is the blocking variant.
It is the password-hashing function built into Node.js, so you can store passwords safely without installing a package. You store the salt, the cost parameters and the derived key, and recompute the key at login.
import { scryptSync, randomBytes } from 'node:crypto'; const salt = randomBytes(16); const key = scryptSync('correct horse', salt, 64);
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.
Syntax
crypto.scrypt(password, salt, keylen, callback): void crypto.scrypt(password, salt, keylen, options, callback): void crypto.scryptSync(password, salt, keylen, options?): Buffer
| Name | Description |
|---|---|
password |
A string, Buffer, typed array or DataView. |
salt |
Same types as password. Should be random and at least 16 bytes. |
keylen |
Number of bytes to produce. |
options.N |
CPU/memory cost. Must be a power of two greater than 1. Default 16384. Alias cost. |
options.r |
Block size. Default 8. Alias blockSize. |
options.p |
Parallelization. Default 1. Alias parallelization. |
options.maxmem |
Upper limit for memory in bytes. Default 33554432 (32 MiB). |
callback |
(err, derivedKey) where derivedKey is a Buffer. |
Returns: scryptSync returns a Buffer of keylen bytes; scrypt returns undefined.
Errors: ERR_CRYPTO_INVALID_SCRYPT_PARAMS when N is not a power of two or the memory needed (about 128 * N * r bytes) exceeds maxmem. ERR_INVALID_ARG_TYPE for wrong argument types.
Usage notes
- Never reuse a salt. Generate 16 or more random bytes per password and store them next to the hash. The salt is not secret.
- Memory use. With the defaults a derivation needs roughly 16 MiB. Doubling
Ndoubles both time and memory; raisemaxmemwhen you raiseNorrbeyond the default limit. - Use the asynchronous form on servers.
scryptSyncblocks the event loop for tens of milliseconds per call, and the asynchronous form runs on the libuv thread pool (four threads by default), so many simultaneous logins can queue up. - Store the parameters. Save
N,r,pand the key length with each hash so you can raise the cost later and re-hash on login. - Compare in constant time. Use
crypto.timingSafeEqual()on buffers of equal length instead of===. - Normalize passwords consistently, for example with
password.normalize('NFKC'), so the same text always produces the same bytes. - Not for general key stretching of high-entropy secrets. For that,
crypto.hkdf()is cheaper; for passwords alone, scrypt, bcrypt and Argon2 are the accepted choices. - Added in Node.js 10.5.0.