public function rsaSign($data, $private_key,$type = 'RSA2') {
$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)
{
if($type == 'RSA'){
openssl_sign($data, $sign,$res);
}elseif($type == 'RSA2'){
//OPENSSL_ALGO_SHA256
openssl_sign($data, $sign,$res,OPENSSL_ALGO_SHA256);
}
openssl_free_key($res);
}else {
exit("私钥格式有误");
}
$sign = base64_encode($sign);
return $sign;