欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

PHP RSA签名

程序员文章站 2022-04-08 13:00:13
/** * RSA签名 * @param $data 待签名数据 * @param $private_key 私钥字符串 * return 签名结果 */function rsaSign($data, $private_key,$sign_type='SHA256') { $search = [ " ......

 /**
 * rsa签名
 * @param $data 待签名数据
 * @param $private_key 私钥字符串
 * return 签名结果
 */
function rsasign($data, $private_key,$sign_type='sha256') {

        $search = [
                "-----begin rsa private key-----",
                "-----end rsa private key-----",
                "\n",
                "\r",
                "\r\n"
        ];

        $private_key=str_replace($search,"",$private_key);
        $private_key=$search[0] . php_eol . wordwrap($private_key, 64, "\n", true) . php_eol . $search[1];
        $res=openssl_get_privatekey($private_key);

        if($res)
        {
                openssl_sign($data, $sign,$res,$sign_type);
                openssl_free_key($res);
        }else {
                exit("私钥格式有误");
        }
        $sign = base64_encode($sign);
        return $sign;
}