crypto.scryptSync(password, salt, keylen[, options])


提供同步 scrypt 实现。Scrypt 是一种基于密码的密钥派生函数,设计上在计算和内存方面成本很高,以使暴力破解攻击得不到回报。

【Provides a synchronous 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.】

salt 应尽可能唯一。建议盐值是随机的,并且至少为 16 字节长。详情请参阅 NIST SP 800-132

【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.】

在传递 passwordsalt 字符串时,请考虑 将字符串用作加密 API 输入时的注意事项

【When passing strings for password or salt, please consider caveats when using strings as inputs to cryptographic APIs.】

当密钥派生失败时,会抛出异常,否则派生的密钥将作为 Buffer 返回。

【An exception is thrown when key derivation fails, otherwise the derived key is returned as a Buffer.】

当任何输入参数指定无效的值或类型时,会抛出异常。

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

const {
  scryptSync,
} = await import('node:crypto');
// Using the factory defaults.

const key1 = scryptSync('password', 'salt', 64);
console.log(key1.toString('hex'));  // '3745e48...08d59ae'
// Using a custom N parameter. Must be a power of two.
const key2 = scryptSync('password', 'salt', 64, { N: 1024 });
console.log(key2.toString('hex'));  // '3745e48...aa39b34'const {
  scryptSync,
} = require('node:crypto');
// Using the factory defaults.

const key1 = scryptSync('password', 'salt', 64);
console.log(key1.toString('hex'));  // '3745e48...08d59ae'
// Using a custom N parameter. Must be a power of two.
const key2 = scryptSync('password', 'salt', 64, { N: 1024 });
console.log(key2.toString('hex'));  // '3745e48...aa39b34'