crypto.scrypt(password, salt, keylen[, options], callback)

  • password {string|Buffer|TypedArray|DataView}
  • salt {string|Buffer|TypedArray|DataView}
  • keylen {number}
  • options {Object}
    • cost {number} CPU/memory cost parameter. Must be a power of two greater than one. Default: 16384.
    • blockSize {number} Block size parameter. Default: 8.
    • parallelization {number} Parallelization parameter. Default: 1.
    • N {number} Alias for cost. Only one of both may be specified.
    • r {number} Alias for blockSize. Only one of both may be specified.
    • p {number} Alias for parallelization. Only one of both may be specified.
    • maxmem {number} Memory upper bound. It is an error when (approximately) 128 * N * r > maxmem. Default: 32 * 1024 * 1024.
  • callback {Function}
    • err {Error}
    • derivedKey {Buffer}

Provides an asynchronous [scrypt][] implementation. Scrypt is a password-based key derivation function that is designed to be expensive computationally and memory-wise in order to make brute-force attacks unrewarding.

The salt should be as unique as possible. It is recommended that a salt is random and at least 16 bytes long. See [NIST SP 800-132][] for details.

The callback function is called with two arguments: err and derivedKey. err is an exception object when key derivation fails, otherwise err is null. derivedKey is passed to the callback as a [Buffer][].

An exception is thrown when any of the input arguments specify invalid values or types.

const crypto = require('crypto');
// Using the factory defaults.
crypto.scrypt('secret', 'salt', 64, (err, derivedKey) => {
  if (err) throw err;
  console.log(derivedKey.toString('hex')); // '3745e48...08d59ae'
});
// Using a custom N parameter. Must be a power of two.
crypto.scrypt('secret', 'salt', 64, { N: 1024 }, (err, derivedKey) => {
  if (err) throw err;
  console.log(derivedKey.toString('hex')); // '3745e48...aa39b34'
});

最后修改 April 16, 2020: 加密 (a75e592)