Next let's take a look at the example program for verifying the signature using openssl_verify in php. I hope this article will be helpful to you.
| The Code is as follows: |
Copy code |
|
<? Php
/**
* Verify the signature
* TobeVerified the ciphertext of the signature to be verified
* PlainText of the signature to be verified
* CertFile: Public Key Certificate of the signatory
* If return is verified successfully, true is returned. If return fails, false is returned (the cause of failure is obtained from the LastErrMsg attribute)
*/
Function VerifyMsg ($ TobeVerified, $ PlainText, $ CertFile, $ signature_alg = OPENSSL_ALGO_SHA1)
{
// Use the public key for Signature Verification
$ Fp = fopen ($ CertFile, "r ");
If (! $ Fp)
{
// Echo "Error Number:-10005, Error Description: ER_FIND_CERT_FAILED (the certificate cannot be found )";
Return false;
}
$ Pub_key = fread ($ fp, 8192 );
Fclose ($ fp );
$ Res = openssl_get_publickey ($ pub_key );
If (1 = openssl_verify ($ PlainText, pack ("H". strlen ($ TobeVerified), $ TobeVerified), $ res, $ signature_alg ))
{
// Print ("www. bKjia. c0m prompts you: Verification Successful". "<br> ");
Return true;
}
Else
{
// Echo "Error Number:-10021, Error Description: ER_VERIFY_ERROR (Signature Verification Failed) |". openssl_error_string ();
Return false;
}
}
?>
|
Openssl_verify may return values 1, 0,-1. Only 1 indicates that the signature is successfully verified.
$ Signature_alg: OPENSSL_ALGO_SHA1 by default. If it is DSA encryption, set it to OPENSSL_ALGO_DSS1.