菜单
文档breadcrumb arrow Grafana k6breadcrumb arrow JavaScript APIbreadcrumb arrow k6/cryptobreadcrumb arrow createHash( algorithm )
开源

createHash( algorithm )

注意

存在一个提供更好、更标准 API 的模块。

The crypto module 部分实现了 WebCrypto API,支持的功能比 k6/crypto 更多。

创建一个哈希对象,可以重复输入数据,并随时从中提取哈希摘要。

参数类型描述
algorithmstring要使用的哈希算法名称。可以是“md4”、“md5”、“sha1”、“sha256”、“sha384”、“sha512”、“sha512_224”、“sha512_256”、“ripemd160”中的任意一个。

返回值

类型描述
object一个 Hasher 对象。

示例

JavaScript
import crypto from 'k6/crypto';

export default function () {
  console.log(crypto.sha256('hello world!', 'hex'));
  const hasher = crypto.createHash('sha256');
  hasher.update('hello ');
  hasher.update('world!');
  console.log(hasher.digest('hex'));
}

以上脚本执行时会打印以下内容

bash
INFO[0000] 7509e5bda0c762d2bac7f90d758b5b2263fa01ccbc542ab5e3df163be08e6ca9
INFO[0000] 7509e5bda0c762d2bac7f90d758b5b2263fa01ccbc542ab5e3df163be08e6ca9