静态方法:KeyObject.from(key)


🌐 Static method: KeyObject.from(key)

返回 <CryptoKey> 的底层 <KeyObject>。返回的 <KeyObject> 不保留 Web Crypto API 对原始 <CryptoKey> 强加的任何限制,例如允许的密钥用途、算法或哈希算法绑定,以及可导出标志。特别是,返回的 <KeyObject> 的底层密钥材料始终可以导出。

🌐 Returns the underlying <KeyObject> of a <CryptoKey>. The returned <KeyObject> does not retain any of the restrictions imposed by the Web Crypto API on the original <CryptoKey>, such as the allowed key usages, the algorithm or hash algorithm bindings, and the extractability flag. In particular, the underlying key material of the returned <KeyObject> can always be exported.

const { KeyObject } = await import('node:crypto');
const { subtle } = globalThis.crypto;

const key = await subtle.generateKey({
  name: 'HMAC',
  hash: 'SHA-256',
  length: 256,
}, true, ['sign', 'verify']);

const keyObject = KeyObject.from(key);
console.log(keyObject.symmetricKeySize);
// Prints: 32 (symmetric key size in bytes)const { KeyObject } = require('node:crypto');
const { subtle } = globalThis.crypto;

(async function() {
  const key = await subtle.generateKey({
    name: 'HMAC',
    hash: 'SHA-256',
    length: 256,
  }, true, ['sign', 'verify']);

  const keyObject = KeyObject.from(key);
  console.log(keyObject.symmetricKeySize);
  // Prints: 32 (symmetric key size in bytes)
})();