类:Hmac
添加于:v0.1.94
Hmac
类是用于创建加密 HMAC 摘要的实用程序。它可以通过以下两种方式之一使用:
- 作为可读可写的stream,其中写入数据以在可读端生成计算的HMAC摘要,或
- 使用
hmac.update()
hmac.digest()
方法用于创建crypto.createHmac()
Hmac
实例。 Hmac
对象不能直接使用new
关键字创建。
示例:使用 Hmac
对象作为流:
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();
示例:使用 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')); // Prints: // 7fd04df92f636fd450bc841c9418e5825c17f33ad9c87c518115a45971f7f77e
const { createHmac, } = require('node:crypto'); const hmac = createHmac('sha256', 'a secret'); hmac.update('some data to hash'); console.log(hmac.digest('hex')); // Prints: // 7fd04df92f636fd450bc841c9418e5825c17f33ad9c87c518115a45971f7f77e
相关用法
- Node.js Http2ServerResponse.finished用法及代码示例
- Node.js Http2Stream close用法及代码示例
- Node.js Http2Session stream用法及代码示例
- Node.js Http2ServerResponse.getHeaderNames()用法及代码示例
- Node.js Http2Session.ping([payload, ]callback)用法及代码示例
- Node.js Http2ServerResponse.statusMessage用法及代码示例
- Node.js Http2ServerResponse.writableEnded用法及代码示例
- Node.js Http2Session timeout用法及代码示例
- Node.js Http2ServerRequest.stream用法及代码示例
- Node.js Http2ServerResponse.setHeader()用法及代码示例
- Node.js Http2ServerRequest.method用法及代码示例
- Node.js Http2ServerResponse.getHeader()用法及代码示例
- Node.js Http2Stream Headers用法及代码示例
- Node.js Http2ServerResponse.addTrailers()用法及代码示例
- Node.js Http2ServerRequest.url用法及代码示例
- Node.js Http2ServerResponse.setTimeout()用法及代码示例
- Node.js Http2ServerRequest.complete用法及代码示例
- Node.js Http2ServerResponse.stream用法及代码示例
- Node.js Http2Stream.setTimeout(msecs, callback)用法及代码示例
- Node.js Http2ServerRequest.destroy()用法及代码示例
- Node.js Http2ServerResponse.removeHeader()用法及代码示例
- Node.js Http2Stream Response用法及代码示例
- Node.js Http2ServerResponse.hasHeader()用法及代码示例
- Node.js Http2ServerRequest.aborted用法及代码示例
- Node.js Http2ServerResponse.getHeaders()用法及代码示例
注:本文由纯净天空筛选整理自nodejs.org大神的英文原创作品 Hmac。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。