菜单
文档breadcrumb arrow Grafana k6breadcrumb arrow JavaScript APIbreadcrumb arrow k6/cryptobreadcrumb arrow sha256( input, outputEncoding )
开源

sha256( input, outputEncoding )

注意

存在一个更好且标准的 API 模块。

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

使用 sha256 对输入数据进行哈希处理。

参数类型描述
input字符串 / ArrayBuffer要进行哈希处理的输入字符串或 ArrayBuffer 对象。
outputEncoding字符串描述用于哈希值的编码类型。可以是“base64”、“base64url”、“base64rawurl”、“hex”或“binary”。

返回

类型描述
字符串 / 数组哈希摘要作为字符串(对于“base64”、“base64url”、“base64rawurl”、“hex” outputEncoding)或原始整数数组(对于“binary” outputEncoding)。

示例

JavaScript
import crypto from 'k6/crypto';

export default function () {
  let hash = crypto.sha256('hello world!', 'hex');
  console.log(hash);
  const binArray = [104, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100, 33];
  hash = crypto.sha256(new Uint8Array(binArray).buffer, 'hex');
  console.log(hash);
}

上述脚本在执行期间应打印以下内容

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