菜单
文档面包屑箭头 Grafana k6面包屑箭头 JavaScript API面包屑箭头 k6/crypto面包屑箭头 sha512( input, outputEncoding )
开源

sha512( input, outputEncoding )

注意

存在一个拥有更好、更标准 API 的模块。

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

使用 sha512 对输入数据进行哈希。

参数类型描述
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.sha512('hello world!', 'hex');
  console.log(hash);
  const binArray = [104, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100, 33];
  hash = crypto.sha512(new Uint8Array(binArray).buffer, 'hex');
  console.log(hash);
}

上述脚本执行后应输出以下结果

bash
INFO[0000] db9b1cd3262dee37756a09b9064973589847caa8e53d31a9d142ea2701b1b28abd97838bb9a27068ba305dc8d04a45a1fcf079de54d607666996b3cc54f6b67c
INFO[0000] db9b1cd3262dee37756a09b9064973589847caa8e53d31a9d142ea2701b1b28abd97838bb9a27068ba305dc8d04a45a1fcf079de54d607666996b3cc54f6b67c