CryptoKey: thuộc tính type

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.

Secure context: This feature is available only in secure contexts (HTTPS), in some or all supporting browsers.

Note: This feature is available in Web Workers.

Thuộc tính chỉ đọc type của giao diện CryptoKey cho biết đối tượng đang biểu diễn loại khóa nào. Thuộc tính này có thể có các giá trị sau:

Giá trị

Một trong các chuỗi sau: "secret", "private", hoặc "public".

Ví dụ

Hàm này xác minh một thông điệp bằng SubtleCrypto.verify() và một khóa công khai được truyền vào tham số. Nếu khóa không phải là khóa công khai, hàm luôn trả về "invalid", vì việc xác minh như vậy về bản chất là không an toàn.

js
async function verifyMessage(publicKey) {
  const signatureValue = document.querySelector(
    ".rsassa-pkcs1 .signature-value",
  );
  signatureValue.classList.remove("valid", "invalid");

  let result = false; // Mặc định là không hợp lệ

  if (publicKey.type === "public") {
    const encoded = getMessageEncoding();
    result = await window.crypto.subtle.verify(
      "RSASSA-PKCS1-v1_5",
      publicKey,
      signature,
      encoded,
    );
  }

  signatureValue.classList.add(result ? "valid" : "invalid");
}

Thông số kỹ thuật

Thông số kỹ thuật
Web Cryptography Level 2
# dom-cryptokey-type

Khả năng tương thích trình duyệt