<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <title>Verify signature</title> </head> <body><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.openssl-sign.html">openssl_sign</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.openssl-x509-check-private-key.html">openssl_x509_check_private_key</a></div> <div class="up"><a href="ref.openssl.html">OpenSSL Functions</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div><hr /><div id="function.openssl-verify" class="refentry"> <div class="refnamediv"> <h1 class="refname">openssl_verify</h1> <p class="verinfo">(PHP 4 >= 4.0.4, PHP 5)</p><p class="refpurpose"><span class="refname">openssl_verify</span> — <span class="dc-title">Verify signature</span></p> </div> <div class="refsect1 description" id="refsect1-function.openssl-verify-description"> <h3 class="title">Description</h3> <div class="methodsynopsis dc-description"> <span class="type">int</span> <span class="methodname"><strong>openssl_verify</strong></span> ( <span class="methodparam"><span class="type">string</span> <code class="parameter">$data</code></span> , <span class="methodparam"><span class="type">string</span> <code class="parameter">$signature</code></span> , <span class="methodparam"><span class="type"><a href="language.pseudo-types.html#language.types.mixed" class="type mixed">mixed</a></span> <code class="parameter">$pub_key_id</code></span> [, <span class="methodparam"><span class="type">int</span> <code class="parameter">$signature_alg</code><span class="initializer"> = OPENSSL_ALGO_SHA1</span></span> ] )</div> <p class="para rdfs-comment"> <span class="function"><strong>openssl_verify()</strong></span> verifies that the <em><code class="parameter">signature</code></em> is correct for the specified <em><code class="parameter">data</code></em> using the public key associated with <em><code class="parameter">pub_key_id</code></em>. This must be the public key corresponding to the private key used for signing. </p> </div> <div class="refsect1 parameters" id="refsect1-function.openssl-verify-parameters"> <h3 class="title">Parameters</h3> <p class="para"> <dl> <dt> <span class="term"><em><code class="parameter">data</code></em></span> <dd> <p class="para"> </p> </dd> </dt> <dt> <span class="term"><em><code class="parameter">signature</code></em></span> <dd> <p class="para"> </p> </dd> </dt> <dt> <span class="term"><em><code class="parameter">pub_key_id</code></em></span> <dd> <p class="para"> </p> </dd> </dt> <dt> <span class="term"><em><code class="parameter">signature_alg</code></em></span> <dd> <p class="para"> For more information see the list of <a href="openssl.signature-algos.html" class="link">Signature Algorithms</a>. </p> </dd> </dt> </dl> </p> </div> <div class="refsect1 returnvalues" id="refsect1-function.openssl-verify-returnvalues"> <h3 class="title">Return Values</h3> <p class="para"> Returns 1 if the signature is correct, 0 if it is incorrect, and -1 on error. </p> </div> <div class="refsect1 changelog" id="refsect1-function.openssl-verify-changelog"> <h3 class="title">Changelog</h3> <p class="para"> <table class="doctable informaltable"> <thead> <tr> <th>Version</th> <th>Description</th> </tr> </thead> <tbody class="tbody"> <tr> <td>5.2.0</td> <td> The <em><code class="parameter">signature_alg</code></em> parameter was added. </td> </tr> </tbody> </table> </p> </div> <div class="refsect1 examples" id="refsect1-function.openssl-verify-examples"> <h3 class="title">Examples</h3> <p class="para"> <div class="example" id="example-871"> <p><strong>Example #1 <span class="function"><strong>openssl_verify()</strong></span> example</strong></p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br /></span><span style="color: #FF8000">// $data and $signature are assumed to contain the data and the signature<br /><br />// fetch public key from certificate and ready it<br /></span><span style="color: #0000BB">$fp </span><span style="color: #007700">= </span><span style="color: #0000BB">fopen</span><span style="color: #007700">(</span><span style="color: #DD0000">"/src/openssl-0.9.6/demos/sign/cert.pem"</span><span style="color: #007700">, </span><span style="color: #DD0000">"r"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$cert </span><span style="color: #007700">= </span><span style="color: #0000BB">fread</span><span style="color: #007700">(</span><span style="color: #0000BB">$fp</span><span style="color: #007700">, </span><span style="color: #0000BB">8192</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">fclose</span><span style="color: #007700">(</span><span style="color: #0000BB">$fp</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$pubkeyid </span><span style="color: #007700">= </span><span style="color: #0000BB">openssl_get_publickey</span><span style="color: #007700">(</span><span style="color: #0000BB">$cert</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">// state whether signature is okay or not<br /></span><span style="color: #0000BB">$ok </span><span style="color: #007700">= </span><span style="color: #0000BB">openssl_verify</span><span style="color: #007700">(</span><span style="color: #0000BB">$data</span><span style="color: #007700">, </span><span style="color: #0000BB">$signature</span><span style="color: #007700">, </span><span style="color: #0000BB">$pubkeyid</span><span style="color: #007700">);<br />if (</span><span style="color: #0000BB">$ok </span><span style="color: #007700">== </span><span style="color: #0000BB">1</span><span style="color: #007700">) {<br /> echo </span><span style="color: #DD0000">"good"</span><span style="color: #007700">;<br />} elseif (</span><span style="color: #0000BB">$ok </span><span style="color: #007700">== </span><span style="color: #0000BB">0</span><span style="color: #007700">) {<br /> echo </span><span style="color: #DD0000">"bad"</span><span style="color: #007700">;<br />} else {<br /> echo </span><span style="color: #DD0000">"ugly, error checking signature"</span><span style="color: #007700">;<br />}<br /></span><span style="color: #FF8000">// free the key from memory<br /></span><span style="color: #0000BB">openssl_free_key</span><span style="color: #007700">(</span><span style="color: #0000BB">$pubkeyid</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> </div> </p> </div> <div class="refsect1 seealso" id="refsect1-function.openssl-verify-seealso"> <h3 class="title">See Also</h3> <p class="para"> <ul class="simplelist"> <li class="member"> <span class="function"><a href="function.openssl-sign.html" class="function" rel="rdfs-seeAlso">openssl_sign()</a> - Generate signature</span></li> </ul> </p> </div> </div><hr /><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.openssl-sign.html">openssl_sign</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.openssl-x509-check-private-key.html">openssl_x509_check_private_key</a></div> <div class="up"><a href="ref.openssl.html">OpenSSL Functions</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div></body></html>