crypto.createHmac(algorithm, key[, options])
曆史
版本 | 變化 |
---|---|
v15.0.0 | key 也可以是ArrayBuffer 或 CryptoKey。添加了編碼選項。 key 不能包含超過 2 ** 32 - 1 個字節。 |
v11.6.0 |
|
v0.1.94 | 添加於:v0.1.94 |
參數
algorithm
<string>key
<string> | <ArrayBuffer> | <Buffer> | <TypedArray> | <DataView> | <KeyObject> | <CryptoKey>options
<Object> new stream.Transform([options])encoding
<string>key
是字符串時使用的字符串編碼。
- 返回: <Hmac>
創建並返回使用給定 algorithm
和 key
的 Hmac
對象。可選的 options
參數控製流行為。
algorithm
取決於平台上OpenSSL 版本支持的可用算法。例如 'sha256'
、 'sha512'
等。在 OpenSSL 的最新版本中,openssl list -digest-algorithms
將顯示可用的摘要算法。
key
是用於生成加密 HMAC 哈希的 HMAC key 。如果是
,則其類型必須是 KeyObject
secret
。
示例:生成文件的 sha256 HMAC
import { createReadStream } from 'fs'; import { argv } from 'node:process'; const { createHmac } = await import('node:crypto'); const filename = argv[2]; const hmac = createHmac('sha256', 'a secret'); const input = createReadStream(filename); input.on('readable', () => { // Only one element is going to be produced by the // hash stream. const data = input.read(); if (data) hmac.update(data); else { console.log(`${hmac.digest('hex')} ${filename}`); } });
const { createReadStream, } = require('node:fs'); const { createHmac, } = require('node:crypto'); const { argv } = require('node:process'); const filename = argv[2]; const hmac = createHmac('sha256', 'a secret'); const input = createReadStream(filename); input.on('readable', () => { // Only one element is going to be produced by the // hash stream. const data = input.read(); if (data) hmac.update(data); else { console.log(`${hmac.digest('hex')} ${filename}`); } });
相關用法
- Node.js crypto.createHmac()用法及代碼示例
- Node.js crypto.createHash()用法及代碼示例
- Node.js crypto.createHash(algorithm[, options])用法及代碼示例
- Node.js crypto.createCipheriv()用法及代碼示例
- Node.js crypto.createVerify()用法及代碼示例
- Node.js crypto.createDiffieHellman()用法及代碼示例
- Node.js crypto.createECDH()用法及代碼示例
- Node.js crypto.createDiffieHellmanGroup()用法及代碼示例
- Node.js crypto.createSign()用法及代碼示例
- Node.js crypto.createDecipheriv()用法及代碼示例
- Node.js crypto.constants用法及代碼示例
- Node.js crypto.checkPrime()用法及代碼示例
- Node.js crypto.checkPrimeSync()用法及代碼示例
- Node.js crypto.randomFill()用法及代碼示例
- Node.js crypto.randomFillSync(buffer[, offset][, size])用法及代碼示例
- Node.js crypto.randomInt([min, ]max[, callback])用法及代碼示例
- Node.js crypto.publicEncrypt()用法及代碼示例
- Node.js crypto.publicDecrypt()用法及代碼示例
- Node.js crypto.pbkdf2Sync(password, salt, iterations, keylen, digest)用法及代碼示例
- Node.js crypto.hkdfSync()用法及代碼示例
- Node.js crypto.randomFillSync()用法及代碼示例
- Node.js crypto.sign()用法及代碼示例
- Node.js crypto.webcrypto用法及代碼示例
- Node.js crypto.generateKeyPairSync(type, options)用法及代碼示例
- Node.js crypto.scrypt(password, salt, keylen[, options], callback)用法及代碼示例
注:本文由純淨天空篩選整理自nodejs.org大神的英文原創作品 crypto.createHmac(algorithm, key[, options])。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。