當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Node.js crypto.scrypt(password, salt, keylen[, options], callback)用法及代碼示例

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

曆史
版本變化
v18.0.0

將無效回調傳遞給 callback 參數現在會拋出 ERR_INVALID_ARG_TYPE 而不是 ERR_INVALID_CALLBACK

v15.0.0

密碼和鹽參數也可以是ArrayBuffer 實例。

v12.8.0、v10.17.0

maxmem 值現在可以是任何安全整數。

v10.9.0

添加了 costblockSizeparallelization 選項名稱。

v10.5.0

添加於:v10.5.0


參數

提供異步 scrypt 實現。 Scrypt 是一種基於密碼的 key 派生函數,其設計目的是在計算和內存方麵成本高昂,以使蠻力攻擊沒有返回。

salt 應盡可能獨特。建議鹽是隨機的並且至少 16 字節長。有關詳細信息,請參閱NIST SP 800-132

在傳遞 passwordsalt 的字符串時,請考慮 caveats when using strings as inputs to cryptographic APIs

callback 函數使用兩個參數調用:errderivedKeyerr 是 key 派生失敗時的異常對象,否則 errnullderivedKey 作為 Buffer 傳遞給回調。

當任何輸入參數指定無效值或類型時,將引發異常。

const {
  scrypt
} = await import('node:crypto');

// Using the factory defaults.
scrypt('password', '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.
scrypt('password', 'salt', 64, { N: 1024 }, (err, derivedKey) => {
  if (err) throw err;
  console.log(derivedKey.toString('hex'));  // '3745e48...aa39b34'
});const {
  scrypt,
} = require('node:crypto');

// Using the factory defaults.
scrypt('password', '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.
scrypt('password', 'salt', 64, { N: 1024 }, (err, derivedKey) => {
  if (err) throw err;
  console.log(derivedKey.toString('hex'));  // '3745e48...aa39b34'
});

相關用法


注:本文由純淨天空篩選整理自nodejs.org大神的英文原創作品 crypto.scrypt(password, salt, keylen[, options], callback)。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。