CryptoKey:algorithm 属性

Baseline 已广泛支持

此特性已相当成熟,可在许多设备和浏览器版本上使用。自 ⁨2015 年 7 月⁩以来,各浏览器均已提供此特性。

安全上下文: 此功能仅在安全上下文(HTTPS)中可用,且支持此功能的浏览器数量有限。

注意:此功能在 Web Workers 中可用。

CryptoKey 接口的只读 algorithm 属性返回一个对象,该对象描述了此密钥可用于的算法以及任何相关的额外参数。

返回的对象取决于用于生成密钥的算法。

如果算法是任何 AES 变体,则返回一个匹配

对于 RsaHashedKeyGenParamsHmacKeyGenParamshash 属性始终采用对象形式(带有一个名为 name 的属性),而不是字符串形式。

示例

js
const rawKey = window.crypto.getRandomValues(new Uint8Array(16));

// Import an AES secret key from an ArrayBuffer containing the raw bytes.
// Takes an ArrayBuffer string containing the bytes, and returns a Promise
// that will resolve to a CryptoKey representing the secret key.
function importSecretKey(rawKey) {
  return window.crypto.subtle.importKey("raw", rawKey, "AES-GCM", true, [
    "encrypt",
    "decrypt",
  ]);
}

const key = importSecretKey(rawKey);
console.log(`This key is to be used with the ${key.algorithm} algorithm.`);

规范

规范
Web 加密级别 2
# dom-cryptokey-algorithm

浏览器兼容性