Php use openssl_verify to verify the signature of the instance program _ PHP Tutorial

Source: Internet
Author: User
Php uses openssl_verify to verify the signature instance program. 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 the code? Php *** to verify the signature, let's take a look at php's use of openssl_verify to verify the signature instance program. I hope this article will be helpful to you.
The code is as follows:


/**

* 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 "."
");
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.

Use openssl_verify to verify the signature instance program. I hope this article will help you. The code is as follows? Php/*** verify the signature...

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.