添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
它可以通过以下两种方式之一使用:

  • 作为既可读又可写的 ,其中写入数据以在可读端生成计算的 HMAC 摘要,或
  • 使用 hmac.update() hmac.digest() 方法生成计算出的 HMAC 摘要。

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

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

const {
  createHmac,
} = await import('node:crypto');
const hmac = createHmac('sha256', 'a secret');
hmac.on('readable', () => {
  // 哈希流只生成
  // 一个元素。
  const data = hmac.read();
  if (data) {
    console.log(data.toString('hex'));
    // 打印:
    //   7fd04df92f636fd450bc841c9418e5825c17f33ad9c87c518115a45971f7f77e
hmac.write('some data to hash');
hmac.end();const {
  createHmac,
} = require('node:crypto');
const hmac = createHmac('sha256', 'a secret');
hmac.on('readable', () => {
  // 哈希流只生成
  // 一个元素。
  const data = hmac.read();
  if (data) {
    console.log(data.toString('hex'));
    // 打印:
    //   7fd04df92f636fd450bc841c9418e5825c17f33ad9c87c518115a45971f7f77e
hmac.write('some data to hash');
hmac.end();

示例:使用 Hmac 和管道流:

import { createReadStream } from 'node:fs';
import { stdout } from 'node:process';
const {
  createHmac,
} = await import('node:crypto');
const hmac = createHmac('sha256', 'a secret');
const input = createReadStream('test.js');
input.pipe(hmac).pipe(stdout);const {
  createReadStream,
} = require('node:fs');
const {
  createHmac,
} = require('node:crypto');
const { stdout } = require('node:process');
const hmac = createHmac('sha256', 'a secret');
const input = createReadStream('test.js');
input.pipe(hmac).pipe(stdout);

示例:使用 hmac.update() hmac.digest() 方法:

const {
  createHmac,
} = await import('node:crypto');
const hmac = createHmac('sha256', 'a secret');
hmac.update('some data to hash');
console.log(hmac.digest('hex'));
// 打印:
//   7fd04df92f636fd450bc841c9418e5825c17f33ad9c87c518115a45971f7f77econst {
  createHmac,
} = require('node:crypto');
const hmac = createHmac('sha256', 'a secret');
hmac.update('some data to hash');
console.log(hmac.digest('hex'));
// 打印:
//   7fd04df92f636fd450bc841c9418e5825c17f33ad9c87c518115a45971f7f77e
Added in: v0.1.94
  • Extends: <stream.Transform>
  • The Hmac class is a utility for creating cryptographic HMAC digests. It can be used in one of two ways:

    • As a stream that is both readable and writable, where data is written to produce a computed HMAC digest on the readable side, or
    • Using the hmac.update() and hmac.digest() methods to produce the computed HMAC digest.

    The crypto.createHmac() method is used to create Hmac instances. Hmac objects are not to be created directly using the new keyword.

    Example: Using Hmac objects as streams:

    const {
      createHmac,
    } = await import('node:crypto');
    const hmac = createHmac('sha256', 'a secret');
    hmac.on('readable', () => {
      // Only one element is going to be produced by the
      // hash stream.
      const data = hmac.read();
      if (data) {
        console.log(data.toString('hex'));
        // Prints:
        //   7fd04df92f636fd450bc841c9418e5825c17f33ad9c87c518115a45971f7f77e
    hmac.write('some data to hash');
    hmac.end();const {
      createHmac,
    } = require('node:crypto');
    const hmac = createHmac('sha256', 'a secret');
    hmac.on('readable', () => {
      // Only one element is going to be produced by the
      // hash stream.
      const data = hmac.read();
      if (data) {
        console.log(data.toString('hex'));
        // Prints:
        //   7fd04df92f636fd450bc841c9418e5825c17f33ad9c87c518115a45971f7f77e
    hmac.write('some data to hash');
    hmac.end();

    Example: Using Hmac and piped streams:

    import { createReadStream } from 'node:fs';
    import { stdout } from 'node:process';
    const {
      createHmac,
    } = await import('node:crypto');
    const hmac = createHmac('sha256', 'a secret');
    const input = createReadStream('test.js');
    input.pipe(hmac).pipe(stdout);const {
      createReadStream,
    } = require('node:fs');
    const {
      createHmac,
    } = require('node:crypto');
    const { stdout } = require('node:process');
    const hmac = createHmac('sha256', 'a secret');
    const input = createReadStream('test.js');
    input.pipe(hmac).pipe(stdout);

    Example: Using the hmac.update() and hmac.digest() methods:

    const {
      createHmac,
    } = await import('node:crypto');
    const hmac = createHmac('sha256', 'a secret');
    hmac.update('some data to hash');
    console.log(hmac.digest('hex'));
    // Prints:
    //   7fd04df92f636fd450bc841c9418e5825c17f33ad9c87c518115a45971f7f77econst {
      createHmac,
    } = require('node:crypto');
    const hmac = createHmac('sha256', 'a secret');
    hmac.update('some data to hash');
    console.log(hmac.digest('hex'));
    // Prints:
    //   7fd04df92f636fd450bc841c9418e5825c17f33ad9c87c518115a45971f7f77e