/** * 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;
}