SubtleCrypto: verify() 方法

Baseline 廣泛可用 *

此特性已相當成熟,可在許多裝置和瀏覽器版本上使用。自 ⁨2020 年 1 月⁩ 起,所有主流瀏覽器均已支援。

* 此特性的某些部分可能存在不同級別的支援。

安全上下文: 此功能僅在安全上下文(HTTPS)中可用,且支援此功能的瀏覽器數量有限。

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

verify() 方法是 SubtleCrypto 介面的一部分,用於驗證數字 簽名

它接受以下引數:用於驗證簽名的 金鑰、特定於演算法的引數、簽名以及原始簽名資料。它返回一個 Promise,該 Promise 將解析為一個布林值,指示簽名是否有效。

語法

js
verify(algorithm, key, signature, data)

引數

algorithm

一個字串或物件,用於定義要使用的演算法,對於某些演算法選擇,還可以包含一些額外的引數。傳遞的額外引數值必須與相應 sign() 呼叫中傳遞的值相匹配。

  • 要使用 RSASSA-PKCS1-v1_5,請傳遞字串 "RSASSA-PKCS1-v1_5" 或形式為 { "name": "RSASSA-PKCS1-v1_5" } 的物件。
  • 要使用 RSA-PSS,請傳遞一個 RsaPssParams 物件。
  • 要使用 ECDSA,請傳遞一個 EcdsaParams 物件。
  • 要使用 HMAC,請傳遞字串 "HMAC" 或形式為 { "name": "HMAC" } 的物件。
  • 要使用 Ed25519,請傳遞形式為 { "name": "Ed25519" } 的物件。
key

一個 CryptoKey,包含用於驗證簽名的金鑰。對於對稱演算法,它是金鑰;對於公鑰系統,它是公鑰。

簽名

一個 ArrayBuffer,包含要驗證的 簽名

data

一個 ArrayBuffer,包含要驗證其簽名的資料。

返回值

一個 Promise,它會解析為一個布林值:如果簽名有效,則為 true,否則為 false

異常

當遇到以下異常時,Promise 將被拒絕

InvalidAccessError DOMException

當加密金鑰不是請求的驗證演算法的金鑰,或者嘗試使用未知或不適合驗證操作的演算法時,會引發此錯誤。

支援的演算法

verify() 方法支援與 sign() 方法相同的演算法。

示例

注意: 您可以在 GitHub 上 嘗試實際可用的示例

RSASSA-PKCS1-v1_5

此程式碼使用公鑰來驗證簽名。在 GitHub 上檢視完整程式碼。

js
/*
Fetch the contents of the "message" textbox, and encode it
in a form we can use for sign operation.
*/
function getMessageEncoding() {
  const messageBox = document.querySelector(".rsassa-pkcs1 #message");
  let message = messageBox.value;
  let enc = new TextEncoder();
  return enc.encode(message);
}

/*
Fetch the encoded message-to-sign and verify it against the stored signature.
* If it checks out, set the "valid" class on the signature.
* Otherwise set the "invalid" class.
*/
async function verifyMessage(publicKey) {
  const signatureValue = document.querySelector(
    ".rsassa-pkcs1 .signature-value",
  );
  signatureValue.classList.remove("valid", "invalid");

  let encoded = getMessageEncoding();
  let result = await window.crypto.subtle.verify(
    "RSASSA-PKCS1-v1_5",
    publicKey,
    signature,
    encoded,
  );

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

RSA-PSS

此程式碼使用公鑰來驗證簽名。在 GitHub 上檢視完整程式碼。

js
/*
Fetch the contents of the "message" textbox, and encode it
in a form we can use for sign operation.
*/
function getMessageEncoding() {
  const messageBox = document.querySelector(".rsa-pss #message");
  let message = messageBox.value;
  let enc = new TextEncoder();
  return enc.encode(message);
}

/*
Fetch the encoded message-to-sign and verify it against the stored signature.
* If it checks out, set the "valid" class on the signature.
* Otherwise set the "invalid" class.
*/
async function verifyMessage(publicKey) {
  const signatureValue = document.querySelector(".rsa-pss .signature-value");
  signatureValue.classList.remove("valid", "invalid");

  let encoded = getMessageEncoding();
  let result = await window.crypto.subtle.verify(
    {
      name: "RSA-PSS",
      saltLength: 32,
    },
    publicKey,
    signature,
    encoded,
  );

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

ECDSA

此程式碼使用公鑰來驗證簽名。在 GitHub 上檢視完整程式碼。

js
/*
Fetch the contents of the "message" textbox, and encode it
in a form we can use for sign operation.
*/
function getMessageEncoding() {
  const messageBox = document.querySelector(".ecdsa #message");
  let message = messageBox.value;
  let enc = new TextEncoder();
  return enc.encode(message);
}

/*
Fetch the encoded message-to-sign and verify it against the stored signature.
* If it checks out, set the "valid" class on the signature.
* Otherwise set the "invalid" class.
*/
async function verifyMessage(publicKey) {
  const signatureValue = document.querySelector(".ecdsa .signature-value");
  signatureValue.classList.remove("valid", "invalid");

  let encoded = getMessageEncoding();
  let result = await window.crypto.subtle.verify(
    {
      name: "ECDSA",
      hash: { name: "SHA-384" },
    },
    publicKey,
    signature,
    encoded,
  );

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

HMAC

此程式碼使用金鑰來驗證簽名。在 GitHub 上檢視完整程式碼。

js
/*
Fetch the contents of the "message" textbox, and encode it
in a form we can use for sign operation.
*/
function getMessageEncoding() {
  const messageBox = document.querySelector(".hmac #message");
  let message = messageBox.value;
  let enc = new TextEncoder();
  return enc.encode(message);
}

/*
Fetch the encoded message-to-sign and verify it against the stored signature.
* If it checks out, set the "valid" class on the signature.
* Otherwise set the "invalid" class.
*/
async function verifyMessage(key) {
  const signatureValue = document.querySelector(".hmac .signature-value");
  signatureValue.classList.remove("valid", "invalid");

  let encoded = getMessageEncoding();
  let result = await window.crypto.subtle.verify(
    "HMAC",
    key,
    signature,
    encoded,
  );

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

Ed25519

Ed25519 即時示例SubtleCrypto.sign() 中演示瞭如何生成公鑰和私鑰,使用私鑰簽名資料,然後使用公鑰驗證簽名。

以下摘錄顯示了與使用公鑰和編碼資料驗證簽名相關的部分。

js
// Verify the signature using the public key
const verifyResult = await crypto.subtle.verify(
  {
    name: "Ed25519",
  },
  publicKey,
  signature,
  encodedData,
);
// True if the signature is valid.

規範

規範
Web 加密級別 2
# SubtleCrypto-method-verify

瀏覽器相容性

另見