Php uses openssl_verify to verify the signature instance Program

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.