PHP uses OpenSSL

Source: Internet
Author: User
Tags openssl

 

code as follows  


<?php 
/** 
 
* Verify signature  
&NBSP
* tobeverified the redaction   to be verified,
 
* PlainText the plaintext   of the signature to be validated;
 
* CertFile signer's public key certificate & nbsp
&NBSP
* Return validation successfully returns True, and the failure returns false (the reason for the failure from the Lasterrmsg property)  
 
*/
Function verifymsg ($ Tobeverified, $PlainText, $CertFile, $signature _alg=openssl_algo_sha1)  

  
  &NBSP;&NBSP;&NBSP
//      $fp =fopen ($CertFile, "R")  
    if (! $fp)  
    { 
        
//echo " Error number:-10005, error description:er_find_cert_failed (certificate not found) "; &NBSP
         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))  
    { 
   &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP
//print ("Www.111cn.net prompts you: Validation succeeded". "<br>");  
         return true; 
   } 
    Else
    { 
        
//echo Error number:-10021, Error Description:er_verify_error (verification failure) | ". Openssl_error_string ();  
        return false; 
    }&NBSP
  

  
?

Openssl_verify may have three return value 1,0,-1, only 1 to indicate successful verification signatures.

$signature _alg default OPENSSL_ALGO_SHA1, if DSA encryption is set 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.