PHP uses OpenSSL to generate signature instance programs

Source: Internet
Author: User

Instance

The code is as follows
<?php


    


/** 


     


* Generate signature content based on original text


     





     


* @param string $data original content


     





     


* @return String


     


*/


function sign ($strData)


    { 


$filePath = ' test.pfx ';


if (!file_exists ($filePath)) {


return false;


        } 


  


$PKCS = file_get_contents ($filePath);


if (Openssl_pkcs12_read ($PKCS, $certs, ')) {


$privateKey = $certs [' Pkey '];


$publicKey = $certs [' cert '];


$SIGNEDMSG = "";


if (openssl_sign ($strData, $SIGNEDMSG, $privateKey)) {


$signedMsg =bin2hex ($SIGNEDMSG);


return $SIGNEDMSG;


} else {


return ";


            } 


} else {


return ' 0 ';


        } 


    } 


    


/* 


  


    


Openssl_pkcs12_read can read the private key in the PFX format without needing to convert to a file in PEM format


    


*/


?>

Openssl_sign default Signature_alg parameter is OPENSSL_ALGO_SHA1

If you use DSA encryption you need to use the OPENSSL_ALGO_DSS1 parameter

Signature_alg Other parameters

Openssl_algo_dss1 (integer)

OPENSSL_ALGO_SHA1 (integer)

openssl_algo_sha224 (integer)

openssl_algo_sha256 (integer)

openssl_algo_sha384 (integer)

openssl_algo_sha512 (integer)

openssl_algo_rmd160 (integer)

OPENSSL_ALGO_MD5 (integer)

OPENSSL_ALGO_MD4 (integer)

OPENSSL_ALGO_MD2 (integer)

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.