• 继承自: {stream.Transform}

    Hmac 类是一个实用工具,用于创建加密的 HMAC 摘要。 它可以通过以下两种方式之一使用:

    • 作为可读写的[流][stream],其中写入数据以在可读侧生成计算后的 HMAC 摘要。
    • 使用 [hmac.update()] 和 [hmac.digest()] 方法生成计算后的 HMAC 摘要。

    [crypto.createHmac()] 方法用于创建 Hmac 实例。 不能使用 new 关键字直接地创建 Hmac 对象。

    示例,使用 Hmac 对象作为流:

    1. const crypto = require('crypto');
    2. const hmac = crypto.createHmac('sha256', '密钥');
    3. hmac.on('readable', () => {
    4. // 哈希流只会生成一个元素。
    5. const data = hmac.read();
    6. if (data) {
    7. console.log(data.toString('hex'));
    8. // 打印:
    9. // d0b5490ab4beb8e6545fe284f484d0d595e46086cb8e6ef2291af12ac684102f
    10. }
    11. });
    12. hmac.write('要创建哈希的数据');
    13. hmac.end();

    示例,使用 Hmac 和管道流:

    1. const crypto = require('crypto');
    2. const fs = require('fs');
    3. const hmac = crypto.createHmac('sha256', '密钥');
    4. const input = fs.createReadStream('要创建哈希的数据.txt');
    5. input.pipe(hmac).pipe(process.stdout);

    示例,使用 [hmac.update()] 和 [hmac.digest()] 方法:

    1. const crypto = require('crypto');
    2. const hmac = crypto.createHmac('sha256', '密钥');
    3. hmac.update('要创建哈希的数据');
    4. console.log(hmac.digest('hex'));
    5. // 打印:
    6. // d0b5490ab4beb8e6545fe284f484d0d595e46086cb8e6ef2291af12ac684102f