crypto.scrypt()
Derives a key from a password using the memory-hard scrypt algorithm.
Syntax
crypto.scrypt(password, salt, keylen[, options], callback) Parameters
| Name | Type | Required | Description |
|---|---|---|---|
password | string | Buffer | Yes | The password to derive a key from. |
salt | string | Buffer | Yes | A unique random salt per password. |
keylen | number | Yes | The desired derived key length in bytes. |
callback | function | Yes | `(err, derivedKey)` callback (use scryptSync for sync). |
Returns
void — Delivers the key via callback; scryptSync returns a Buffer.
Examples
import { scrypt, randomBytes } from 'node:crypto';
import { promisify } from 'node:util';
const scryptAsync = promisify(scrypt);
const salt = randomBytes(16);
const hash = await scryptAsync('hunter2', salt, 64);
console.log(hash.toString('hex').slice(0, 16));
Output
a3f1c8e2b7d49a6e
import { scryptSync, timingSafeEqual } from 'node:crypto';
function check(password, salt, stored) {
const h = scryptSync(password, salt, 64);
return timingSafeEqual(h, stored);
}
Notes
Designed for password hashing: store the salt alongside the hash and
use a fresh random salt per user. Verify with `timingSafeEqual`. The
async form avoids blocking the event loop under this CPU-heavy work.