Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to implement SHA, SHA2 encryption and Verification with openssl_sign in PHP

Shulou Source: shulou.com Published: 2022-06-02 08:56:24 10月16日 Update

This article mainly introduces "how PHP uses openssl_sign to achieve SHA, SHA2 encryption and authentication". In daily operation, I believe many people have doubts about how PHP uses openssl_sign to achieve SHA, SHA2 encryption and verification. Xiaobian consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the doubts of "how PHP uses openssl_sign to achieve SHA, SHA2 encryption and authentication". Next, please follow the editor to study!

This time, in conjunction with the Java server, the other party uses SHA256WithRSA encryption. At first, it is thought to be the default signature algorithm corresponding to the openssl_sign () function of php, but later found that it is not, so the record avoids trampling later.

Class RSA {$public_key = 'aaaaaaaa'; $private_key =' bbbbbb';/*** format public and private keys * $key string public or private keys * $type string public | private * / public function formatKey ($key,$type = 'public') {$key= chunk_split ($key,64, "\ n") If ('public' = = $type) {$key= "- BEGIN RSA PRIVATE KEY-\ n". $key. "- END RSA PRIVATE KEY-\ n";} else {$key= "- BEGIN RSA PRIVATE KEY-\ n". $key. "- END RSA PRIVATE KEY-\ n";} return $key } / * encrypt * $encode_str string string to be encrypted * $key string public or private key, usually with the private key * $type string OPENSSL_ALGO_SHA1 | OPENSSL_ALGO_SHA256 corresponds to sha1 and sha2*/public function encode ($encode_str,$key,$type = OPENSSL_ALGO_SHA1) {$result = false; if (openssl_sign ($data, $result, $key,$type)) {$result = base64_encode ('. $result);} return $result } / * verify * $verfiy_str string string to be verified * $key string public or private key, usually using the public key * $type string OPENSSL_ALGO_SHA1 | OPENSSL_ALGO_SHA256 corresponds to sha1 and sha2*/public function verfiy ($verfiy_str,$sign,$key,$type = OPENSSL_ALGO_SHA1) {return openssl_verify ($verfiy_str,base64_decode ($sign), $key,$type) }} at this point, the study on "how PHP uses openssl_sign to achieve SHA, SHA2 encryption and authentication" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Tags: Encryption authentication public key learning characters strings more help practical next functions each other articles methods formats theories knowledge algorithms articles websites Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Apple Huawei macOS Shulou Information Docker