SubtleCrypto: verify() 方法
Baseline 广泛可用 *
注意:此功能在 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" }的对象。
- 要使用 RSASSA-PKCS1-v1_5,请传递字符串
key-
一个
CryptoKey,包含用于验证签名的密钥。对于对称算法,它是密钥;对于公钥系统,它是公钥。 签名-
一个
ArrayBuffer,包含要验证的 签名。 data-
一个
ArrayBuffer,包含要验证其签名的数据。
返回值
一个 Promise,它会解析为一个布尔值:如果签名有效,则为 true,否则为 false。
异常
当遇到以下异常时,Promise 将被拒绝
InvalidAccessErrorDOMException-
当加密密钥不是请求的验证算法的密钥,或者尝试使用未知或不适合验证操作的算法时,会引发此错误。
支持的算法
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 |
浏览器兼容性
加载中…
另见
SubtleCrypto.sign().- RFC 3447 规定了 RSASSA-PKCS1-v1_5。
- RFC 3447 规定了 RSA-PSS。
- FIPS-186 规定了 ECDSA。
- FIPS 198-1 指定了 HMAC。