菜单
开源

JsonWebKey

注意

实验性模块 k6/experimental/webcrypto 已毕业,其功能现在可通过 crypto 对象全局可用。k6/experimental/webcrypto 已弃用,并将在不久的将来移除。

要迁移您的脚本,请移除 k6/experimental/webcrypto 导入并改用 crypto 对象。

JsonWebKey 对象表示通过导出 CryptoKey 或用作密钥导入输入参数生成的对象/字典。

JsonWebKey 的属性可能会因算法和密钥类型而异。详情请参阅规范 JsonWebKey

属性

属性类型描述
ktystring密钥类型。
kstring密钥值。
algstring算法。
extbool密钥可提取。
key_opsstring 数组密钥操作。
crvstring曲线名称。
xstringx 坐标。
ystringy 坐标。
dstring私钥。

示例

JavaScript
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
}