CryptoKey:algorithm 屬性
注意:此功能在 Web Workers 中可用。
CryptoKey 介面的只讀 algorithm 屬性返回一個物件,該物件描述了此金鑰可用於的演算法以及任何相關的額外引數。
返回的物件取決於用於生成金鑰的演算法。
值
如果演算法是任何 AES 變體,則返回一個匹配
AesKeyGenParams的物件。- 如果演算法是任何 RSA 變體,則返回一個匹配
RsaHashedKeyGenParams的物件。 - 如果演算法是任何 EC 變體,則返回一個匹配
EcKeyGenParams的物件。 - 如果演算法是 HMAC,則返回一個匹配
HmacKeyGenParams的物件。
對於 RsaHashedKeyGenParams 和 HmacKeyGenParams,hash 屬性始終採用物件形式(帶有一個名為 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 |
瀏覽器相容性
載入中…