阿西河

所有教程

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

我的收藏

    最近访问  (文章)

      教程列表

      抓包专区
      测试专区

      Node.js crypto.createHmac

      Node.js crypto.createHmac(algorithm, key[, options])

      新增于: v0.1.94

      • algorithm < string>
      • key < string> | < Buffer> ||
      • options < Object> stream.transform options
      • Returns:

      Creates and returns an Hmac object that uses the given algorithm and key. Optional options argument controls stream behavior.

      The algorithm is dependent on the available algorithms supported by the version of OpenSSL on the platform. Examples are ‘sha256’, ‘sha512’, etc. On recent releases of OpenSSL, openssl list -digest-algorithms (openssl list-message-digest-algorithms for older versions of OpenSSL) will display the available digest algorithms.

      The key is the HMAC key used to generate the cryptographic HMAC hash.

      Example: generating the sha256 HMAC of a file

      const filename = process.argv[2];
      const crypto = require('crypto');
      const fs = require('fs');
      
      const hmac = crypto.createHmac('sha256', 'a secret');
      
      const input = fs.createReadStream(filename);
      input.on('readable', () => {
        const data = input.read();
        if (data)
          hmac.update(data);
        else {
          console.log(`${hmac.digest('hex')} ${filename}`);
        }
      });
      

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


      目录
      本文目录
      目录