PHP RSA2 signature algorithm

*文
Release: 2023-03-18 08:26:02
Original
4296 people have browsed it

The RSA2 signature algorithm will be used in some projects. Today we will look at the use of the RSA2 signature algorithm in PHP.

What is RSA2?

RSA2 is based on the original SHA1WithRSA signature algorithm and adds a new signature algorithm that supports SHA256WithRSA.

This algorithm has stronger security capabilities than SHA1WithRSA in terms of digest algorithm.

The SHA1WithRSA signature algorithm will continue to be supported, but for the security of your application, it is strongly recommended to use the SHA256WithRSA signature algorithm.

What companies are using it?

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

Create private key and public key

//生成原始 RSA私钥文件
openssl genrsa -out rsa_private_key.pem 1024
//将原始 RSA私钥转换为 pkcs8格式
openssl pkcs8 -topk8 -inform PEM -in rsa_private_key.pem -outform PEM -nocrypt -out private_key.pem
//生成RSA公钥
openssl rsa -in rsa_private_key.pem -pubout -out rsa_public_key.pem
//我们将私钥rsa_private_key.pem用在服务器端,公钥发放给android跟ios等前端。
Copy after login

PHP-RSA2 signature verification

class Rsa2
{
    private static $PRIVATE_KEY = 'rsa_private_key.pem 内容';
    private static $PUBLIC_KEY  = 'rsa_public_key.pem 内容';
    /**
     * 获取私钥
     * @return bool|resource
     */
    private static function getPrivateKey()
    {
        $privKey = self::$PRIVATE_KEY;
        return openssl_pkey_get_private($privKey);
    }
    /**
     * 获取公钥
     * @return bool|resource
     */
    private static function getPublicKey()
    {
        $publicKey = self::$PUBLIC_KEY;
        return openssl_pkey_get_public($publicKey);
    }
    /**
     * 创建签名
     * @param string $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;
    }
    /**
     * 验证签名
     * @param string $data 数据
     * @param string $sign 签名
     * @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
                    );
    }
}
Copy after login

PHP call

require_once "Rsa2.php";
$rsa2 = new Rsa2();
$data = 'my data'; //待签名字符串
$strSign = $rsa2->createSign($data);      //生成签名
var_dump($strSign);
$is_ok = $rsa2->verifySign($data, $sign); //验证签名
var_dump($is_ok);
Copy after login

Related reading:

Example analysis of binary search algorithm implemented in PHP

How PHP implements the Naive Bayes algorithm for machine learning

Detailed analysis of how PHP implements the Hill sorting algorithm

The above is the detailed content of PHP RSA2 signature algorithm. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!