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 |
浏览器兼容性
加载中…