llpay_rsa.php 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: tandunzhao
  5. * Date: 2017/12/27
  6. * Time: 下午8:04
  7. */
  8. namespace App\Libs\lianlianpay;
  9. class llpay_rsa
  10. {
  11. /**
  12. * RSA签名
  13. * @param $data 签名数据(需要先排序,然后拼接)
  14. * 签名用商户私钥,必须是没有经过pkcs8转换的私钥
  15. * 最后的签名,需要用base64编码
  16. * @param $priKey
  17. * @return string
  18. */
  19. public static function Rsasign($data,$priKey) {
  20. //转换为openssl密钥,必须是没有经过pkcs8转换的私钥
  21. $res = openssl_get_privatekey($priKey);
  22. // file_put_contents("log.txt","签名res:".$res."\n", FILE_APPEND);
  23. //调用openssl内置签名方法,生成签名$sign
  24. openssl_sign($data, $sign, $res,OPENSSL_ALGO_MD5);
  25. //释放资源
  26. openssl_free_key($res);
  27. //base64编码
  28. $sign = base64_encode($sign);
  29. // file_put_contents("log.txt","签名原串:".$data."\n", FILE_APPEND);
  30. // file_put_contents("log.txt","签名结果:".$sign."\n", FILE_APPEND);
  31. return $sign;
  32. }
  33. /********************************************************************************/
  34. /**
  35. * RSA验签
  36. * @param $data 待签名数据(需要先排序,然后拼接)
  37. * @param $sign 需要验签的签名,需要base64_decode解码 验签用连连支付公钥
  38. * @return bool 验签是否通过 bool值
  39. */
  40. public static function Rsaverify($data, $sign) {
  41. //读取连连支付公钥文件
  42. $pubKey = file_get_contents('key/llpay_public_key.pem');
  43. //转换为openssl格式密钥
  44. $res = openssl_get_publickey($pubKey);
  45. //调用openssl内置方法验签,返回bool值
  46. $result = (bool)openssl_verify($data, base64_decode($sign), $res,OPENSSL_ALGO_MD5);
  47. //释放资源
  48. openssl_free_key($res);
  49. //返回资源是否成功
  50. return $result;
  51. }
  52. }