阿西河

所有教程

公众号
🌙
阿西河前端的公众号

我的收藏

    最近访问  (文章)

      教程列表

      抓包专区
      测试专区

      Node.js crypto.pbkdf2

      Node.js crypto.pbkdf2(password, salt, iterations, keylen, digest, callback)版本历史

      版本变更
      v8.0.0The digest parameter is always required now.
      v6.0.0Calling this function without passing the digest parameter is deprecated now and will emit a warning.
      v6.0.0The default encoding for password if it is a string changed from binary to utf8.
      v0.5.5新增于: v0.5.5
      • password < string> | < Buffer> ||
      • salt < string> | < Buffer> ||
      • iterations < number>
      • keylen < number>
      • digest < string>
      • callback < Function>
        • err < Error>
        • derivedKey < Buffer>

      Provides an asynchronous Password-Based Key Derivation Function 2 (PBKDF2) implementation. A selected HMAC digest algorithm specified by digest is applied to derive a key of the requested byte length (keylen) from the password, salt and iterations.

      The supplied callback function is called with two arguments: err and derivedKey. If an error occurs while deriving the key, err will be set; otherwise err will be null. By default, the successfully generated derivedKey will be passed to the callback as a Buffer. An error will be thrown if any of the input arguments specify invalid values or types.

      If digest is null, ‘sha1’ will be used. This behavior will be deprecated in a future version of Node.js.

      The iterations argument must be a number set as high as possible. The higher the number of iterations, the more secure the derived key will be, but will take a longer amount of time to complete.

      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.

      const crypto = require('crypto');
      crypto.pbkdf2('secret', 'salt', 100000, 64, 'sha512', (err, derivedKey) => {
        if (err) throw err;
        console.log(derivedKey.toString('hex'));  // '3745e48...08d59ae'
      });
      

      The crypto.DEFAULT_ENCODING property can be used to change the way the derivedKey is passed to the callback. This property, however, has been deprecated and use should be avoided.

      const crypto = require('crypto');
      crypto.DEFAULT_ENCODING = 'hex';
      crypto.pbkdf2('secret', 'salt', 100000, 512, 'sha512', (err, derivedKey) => {
        if (err) throw err;
        console.log(derivedKey);  // '3745e48...aa39b34'
      });
      

      An array of supported digest functions can be retrieved using crypto.getHashes().

      Note that this API uses libuv’s threadpool, which can have surprising and negative performance implications for some applications, see the UV_THREADPOOL_SIZE documentation for more information.


      更多内容请参考:Node.js crypto 加密,或者通过 点击对应菜单 进行查看;


      目录
      本文目录
      目录