createHMAC( algorithm, secret )
注意
存在一个提供更好、更标准 API 的模块。
crypto 模块部分实现了 WebCrypto API,支持的功能比 k6/crypto 更多。
创建一个 HMAC 哈希对象,该对象可以重复接收数据,并可在需要时从中提取签名的哈希摘要。
参数 | 类型 | 说明 |
---|---|---|
algorithm | 字符串 | 要使用的哈希算法。可以是 md4 , md5 , sha1 , sha256 , sha384 , sha512 , sha512_224 , sha512_256 或 ripemd160 中的一种。 |
secret | 字符串 / ArrayBuffer | 用于签名数据的共享密钥。 |
返回值
类型 | 说明 |
---|---|
对象 | 一个 Hasher 对象。 |
示例
import crypto from 'k6/crypto';
export default function () {
console.log(crypto.hmac('sha256', 'a secret', 'my data', 'hex'));
const hasher = crypto.createHMAC('sha256', 'a secret');
hasher.update('my ');
hasher.update('data');
console.log(hasher.digest('hex'));
}
上述脚本执行时应输出以下内容
INFO[0000] 82f669c8fde13aef6d6977257588dc4953dfac505428f8fd6b52e19cd96d7ea5
INFO[0000] 82f669c8fde13aef6d6977257588dc4953dfac505428f8fd6b52e19cd96d7ea5