← 返回首页
SubtleCrypto: verify() メソッド - Web API | MDN

このページはコミュニティーの尽力で英語から翻訳されました。MDN Web Docs コミュニティーについてもっと知り、仲間になるにはこちらから。

View in English Always switch to English

SubtleCrypto: verify() メソッド

Baseline Widely available *

This feature is well established and works across many devices and browser versions. It’s been available across browsers since 2020年1月.

* Some parts of this feature may have varying levels of support.

安全なコンテキスト用: この機能は一部またはすべての対応しているブラウザーにおいて、安全なコンテキスト (HTTPS) でのみ利用できます。

verify()SubtleCrypto インターフェイスのメソッドで、デジタル署名を検証します。

引数として、署名を検証するための、アルゴリズム固有の引数、署名、署名済み元データを取ります。署名が有効かどうかを示す論理値で履行される Promise を返します。

In this article

構文

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" } の形の文字列を渡してください。
key

署名に用いる鍵を格納した CryptoKey オブジェクトです。 対称鍵アルゴリズムであれば秘密鍵であり、公開鍵システムであれば公開鍵です。

signature

ArrayBuffer で、検証する署名です。

data

ArrayBuffer で、署名を検証するためのデータが入ります。

返値

論理値で履行される Promise です。署名が有効な場合は true、そうでない場合は false です。

例外

以下の例外が発生した場合、プロミスは拒否されます。

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"); }

仕様書

Specification
Web Cryptography Level 2
# SubtleCrypto-method-verify

ブラウザーの互換性

Enable JavaScript to view this browser compatibility table.

関連情報