Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

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

2025-01-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report