randomBytes( int )
注意
存在一个拥有更好、更标准 API 的模块。
crypto 模块部分实现了 WebCrypto API,支持的功能比 k6/crypto 更多。
返回一个包含指定数量的密码学随机字节的 ArrayBuffer 对象。它将精确返回请求的字节数,或者在发生错误时抛出异常。
参数 | 类型 | 描述 |
---|---|---|
int | 整数 | 返回的 ArrayBuffer 的长度。 |
返回值
类型 | 描述 |
---|---|
ArrayBuffer | 包含密码学随机字节的 ArrayBuffer。 |
示例
import crypto from 'k6/crypto';
export default function () {
const bytes = crypto.randomBytes(42);
const view = new Uint8Array(bytes);
console.log(view); // 156,71,245,191,56,...
}