JsonWebKey
注意
实验性模块
k6/experimental/webcrypto
已毕业,其功能现在可通过crypto
对象全局可用。k6/experimental/webcrypto
已弃用,并将在不久的将来移除。要迁移您的脚本,请移除
k6/experimental/webcrypto
导入并改用crypto
对象。
JsonWebKey
对象表示通过导出 CryptoKey
或用作密钥导入输入参数生成的对象/字典。
JsonWebKey
的属性可能会因算法和密钥类型而异。详情请参阅规范 JsonWebKey。
属性
属性 | 类型 | 描述 |
---|---|---|
kty | string | 密钥类型。 |
k | string | 密钥值。 |
alg | string | 算法。 |
ext | bool | 密钥可提取。 |
key_ops | string 数组 | 密钥操作。 |
crv | string | 曲线名称。 |
x | string | x 坐标。 |
y | string | y 坐标。 |
d | string | 私钥。 |
示例
import { crypto } from 'k6/experimental/webcrypto';
export default async function () {
const jwk = {
alg: 'HS256',
ext: true,
k: 'H6gLp3lw7w27NrPUn00WpcKU-IJojJdNzhL_8F6se2k',
key_ops: ['sign', 'verify'],
kty: 'oct',
};
const importedKey = await crypto.subtle.importKey(
'jwk',
jwk,
{ name: 'HMAC', hash: { name: 'SHA-256' } },
true,
['sign', 'verify']
);
const exportedAgain = await crypto.subtle.exportKey('jwk', importedKey);
console.log('exported again: ' + JSON.stringify(exportedAgain));
// should print
// INFO[0000] exported again: {"k":"H6gLp3lw7w27NrPUn00WpcKU-IJojJdNzhL_8F6se2k","kty":"oct","ext":true,"key_ops":["sign","verify"],"alg":"HS256"} source=console
}