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

瀏覽器相容性