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

What is the PHP RSA2 signature algorithm?

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article shows you how the PHP RSA2 signature algorithm is, the content is concise and easy to understand, absolutely can make your eyes bright, through the detailed introduction of this article, I hope you can get something.

What is RSA2?

RSA2 adds a new signature algorithm that supports SHA256WithRSA on the basis of the original SHA1WithRSA signature algorithm.

This algorithm has stronger security capability than SHA1WithRSA.

For the security of your application, it is strongly recommended to use SHA256WithRSA's signature algorithm.

Development platform algorithm name Standard signature algorithm name remarks RSA2SHA256WithRSA (highly recommended). The length of RSA key is required to be at least 2048RSASHA1WithRSA. There is no limit to the length of RSA key. It is recommended to use more than 2048 bits.

Which companies are using it?

Development platforms of some large companies, such as Alipay and Sina Weibo.

Create private key and public key

/ / generate the original RSA private key file

Openssl genrsa-out rsa_private_key.pem 1024

/ / convert the original RSA private key to pkcs8 format

Openssl pkcs8-topk8-inform PEM-in rsa_private_key.pem-outform PEM-nocrypt-out private_key.pem

/ / generate RSA public key

Openssl rsa-in rsa_private_key.pem-pubout-out rsa_public_key.pem

PHP-RSA2 signature verification

Class Rsa2 {private static $PRIVATE_KEY = 'rsa_private_key.pem content'; private static $PUBLIC_KEY = 'rsa_public_key.pem content'; / * * obtain private key * @ return bool | resource * / private static function getPrivateKey () {$privKey = self::$PRIVATE_KEY; return openssl_pkey_get_private ($privKey) } / * get the public key * @ return bool | resource * / private static function getPublicKey () {$publicKey = self::$PUBLIC_KEY; return openssl_pkey_get_public ($publicKey) } / * create signature * @ param string $data data * @ return null | string * / public function createSign ($data =') {if (! is_string ($data)) {return null } return openssl_sign ($data, $sign, self::getPrivateKey (), OPENSSL_ALGO_SHA256)? Base64_encode ($sign): null;} / * verify signature * @ param string $data data * @ param string $sign signature * @ return bool * / public function verifySign ($data ='', $sign ='') {if (! is_string ($sign) | |! is_string ($sign)) {return false } return (bool) openssl_verify ($data, base64_decode ($sign), self::getPublicKey (), OPENSSL_ALGO_SHA256);}}

PHP call

Require_once "Rsa2.php"; $rsa2 = new Rsa2 (); $data ='my data'; / / string to be signed $strSign = $rsa2- > createSign ($data); / / generate signature var_dump ($strSign); $is_ok = $rsa2- > verifySign ($data, $sign); / / verify signature var_dump ($is_ok); the above is what PHP RSA2 signature algorithm is like, have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.

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