CryptoKey:usages 属性

Baseline 已广泛支持

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

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

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

CryptoKey 接口的只读属性 usages 指示了该密钥可用于执行的操作。

一个 Array,其中包含以下列表中的字符串:

  • "encrypt":该密钥可用于加密消息。
  • "decrypt":该密钥可用于解密消息。
  • "sign":该密钥可用于签名消息。
  • "verify":该密钥可用于验证签名。
  • "deriveKey":该密钥可用于派生新密钥
  • "deriveBits":该密钥可用于派生位
  • "wrapKey":该密钥可用于包装密钥
  • "unwrapKey":该密钥可用于解包密钥

示例

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(
  `The following usages are reported for this key: ${key.usages.toString()}`,
);

规范

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

浏览器兼容性