安全上下文
此特征只可用于
安全上下文
(HTTPS),在某些或所有
支持浏览器
.
verify()
方法在
SubtleCrypto
interface verifies a digital
signature
.
It takes as its arguments a
key
to verify the signature with, some algorithm-specific parameters, the signature, and the original signed data. It returns a
Promise
which will be fulfilled with a
布尔
value indicating whether the signature is valid.
const result = crypto.subtle.verify(algorithm, key, signature, data);
algorithm
是
DOMString
or object defining the algorithm to use, and for some algorithm choices, some extra parameters. The values given for the extra parameters must match those passed into the corresponding
sign()
调用。
"RSASSA-PKCS1-v1_5"
or an object of the form
{ "name": "RSASSA-PKCS1-v1_5" }
.
RsaPssParams
对象。
EcdsaParams
对象。
"HMAC"
or an object of the form
{ "name": "HMAC" }
.
key
是
CryptoKey
containing the key that will be used to verify the signature. It is the secret key for a symmetric algorithm and the public key for a public-key system.
signature
是
ArrayBuffer
containing the
signature
to verify.
data
是
ArrayBuffer
containing the data whose signature is to be verified.
The promise is rejected when the following exception is encountered:
InvalidAccessError
Raised when the encryption key is not a key for the requested verifying algorithm or when trying to use an algorithm that is either unknown or isn't suitable for a verify operation.
verify()
method supports the same algorithms as the
sign()
方法。
注意 : You can try the working examples out on GitHub.
This code uses a public key to verify a signature. See the complete code on GitHub.
/*
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");
}
This code uses a public key to verify a signature. See the complete code on GitHub.
/*
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");
}
This code uses a public key to verify a signature. See the complete code on GitHub.
/*
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");
}
This code uses a secret key to verify a signature. See the complete code on GitHub.
/*
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");
}
| 规范 | 状态 | 注释 |
|---|---|---|
|
Web Cryptography API
The definition of 'SubtleCrypto.verify()' in that specification. |
推荐 | 初始定义。 |
| 桌面 | 移动 | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
verify
|
Chrome 37 |
Edge
部分支持
12
|
Firefox
34
|
IE
部分支持
11
|
Opera 24 | Safari 7 | WebView Android 37 | Chrome Android 37 |
Firefox Android
34
|
Opera Android 24 | Safari iOS 7 | Samsung Internet Android 6.0 |
完整支持
部分支持
见实现注意事项。
用户必须明确启用此特征。
SubtleCrypto.sign()
.
SubtleCrypto