/**
* 证书加密
* @param string $data 字符串
* @param string $pfxpath 证书地址
* @param string $pfxpwd 证书密码
* @return string|void
*/
public function sign($data,$pfxpath,$pfxpwd) {
$certs= array();
openssl_pkcs12_read(file_get_contents($pfxpath), $certs,$pfxpwd);
if (!$certs) {
return;
}
$signature= '';
openssl_sign($data, $signature, $certs['pkey']);
return base64_encode($signature);
}
/**
* 验证自己生成的签名是否正确
* @param string $data 签名前原字符串
* @param string $signature 签名字符串
* @return int|void
*/
public function verify($data,$signature,$pfxpath,$pfxpwd) {
$certs= array();
openssl_pkcs12_read(file_get_contents($pfxpath), $certs,$pfxpwd);
if(!$certs) {
return;
}
// openssl_verify验签成功返回1,失败0,错误返回-1
$result= openssl_verify($data,base64_decode($signature), $certs['cert']);
return $result;
}