當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Node.js KeyObject.from(key)用法及代碼示例

靜態方法:KeyObject.from(key)

添加於:v15.0.0

參數

示例:將 CryptoKey 實例轉換為 KeyObject

const { webcrypto, KeyObject } = await import('node:crypto');
const { subtle } = webcrypto;

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 {
  webcrypto: {
    subtle,
  },
  KeyObject,
} = require('node: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)
})();

相關用法


注:本文由純淨天空篩選整理自nodejs.org大神的英文原創作品 KeyObject.from(key)。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。