類: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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。