菜单
开源

JsonWebKey

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

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

属性

属性类型描述
kty字符串密钥类型。
k字符串密钥值。
alg字符串算法。
ext布尔值密钥是可提取的。
key_ops字符串数组密钥操作。
crv字符串曲线名称。
x字符串x 坐标。
y字符串y 坐标。
d字符串私钥。

示例

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