AesCtrParams
注意
实验模块
k6/experimental/webcrypto
已升级,其功能现在通过crypto
对象全局可用。k6/experimental/webcrypto
已弃用,并将在不久的将来移除。要迁移您的脚本,请移除
k6/experimental/webcrypto
导入,并改用crypto
对象。
AesCtrParams
对象表示在使用 AES-CTR 算法时,应作为 algorithm 参数传递给 encrypt 和 decrypt 操作的对象。
更多详细信息,请参阅 MDN Web Crypto API 关于 AES-CTR 的文档。
属性
属性 | 类型 | 描述 |
---|---|---|
name | string | 应设置为 AES-CTR 。 |
counter | ArrayBuffer , TypedArray , 或 DataView | 计数器块的初始值。必须是 16 字节长,与 AES 块大小相同。 |
length | number | 计数器块中用于实际计数器的位数。建议使用 64 位(计数器块的一半)。 |
示例
import { crypto } from 'k6/experimental/webcrypto';
export default async function () {
const plaintext = stringToArrayBuffer('Hello, World!');
/**
* Generate a symmetric key using the AES-CTR algorithm.
*/
const key = await crypto.subtle.generateKey(
{
name: 'AES-CTR',
length: 256,
},
true,
['encrypt', 'decrypt']
);
/**
* Encrypt the plaintext using the AES-CTR key with
* have generated.
*/
const ciphertext = await crypto.subtle.encrypt(
{
name: 'AES-CTR',
counter: crypto.getRandomValues(new Uint8Array(16)),
length: 128,
},
key,
plaintext
);
}
function stringToArrayBuffer(str) {
const buf = new ArrayBuffer(str.length * 2); // 2 bytes for each char
const bufView = new Uint16Array(buf);
for (let i = 0, strLen = str.length; i < strLen; i++) {
bufView[i] = str.charCodeAt(i);
}
return buf;
}