12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364 |
- <?php
- /**
- * Created by PhpStorm.
- * User: tandunzhao
- * Date: 2017/12/27
- * Time: 下午8:04
- */
- namespace App\Libs\lianlianpay;
- class llpay_rsa
- {
- /**
- * RSA签名
- * @param $data 签名数据(需要先排序,然后拼接)
- * 签名用商户私钥,必须是没有经过pkcs8转换的私钥
- * 最后的签名,需要用base64编码
- * @param $priKey
- * @return string
- */
- public static function Rsasign($data,$priKey) {
- //转换为openssl密钥,必须是没有经过pkcs8转换的私钥
- $res = openssl_get_privatekey($priKey);
- // file_put_contents("log.txt","签名res:".$res."\n", FILE_APPEND);
- //调用openssl内置签名方法,生成签名$sign
- openssl_sign($data, $sign, $res,OPENSSL_ALGO_MD5);
- //释放资源
- openssl_free_key($res);
- //base64编码
- $sign = base64_encode($sign);
- // file_put_contents("log.txt","签名原串:".$data."\n", FILE_APPEND);
- // file_put_contents("log.txt","签名结果:".$sign."\n", FILE_APPEND);
- return $sign;
- }
- /********************************************************************************/
- /**
- * RSA验签
- * @param $data 待签名数据(需要先排序,然后拼接)
- * @param $sign 需要验签的签名,需要base64_decode解码 验签用连连支付公钥
- * @return bool 验签是否通过 bool值
- */
- public static function Rsaverify($data, $sign) {
- //读取连连支付公钥文件
- $pubKey = file_get_contents('key/llpay_public_key.pem');
- //转换为openssl格式密钥
- $res = openssl_get_publickey($pubKey);
- //调用openssl内置方法验签,返回bool值
- $result = (bool)openssl_verify($data, base64_decode($sign), $res,OPENSSL_ALGO_MD5);
- //释放资源
- openssl_free_key($res);
- //返回资源是否成功
- return $result;
- }
- }
|