joloRsa.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. <?php
  2. namespace ComplexThree\JuLe;
  3. /**
  4. * Class joloRsa
  5. * rsa加密工具类
  6. */
  7. class joloRsa
  8. {
  9. /**
  10. * $orderArr = [];
  11. * $orderArr['game_name'] = 'game_name'; //游戏名称
  12. * $orderArr['game_code'] = 'game_code'; //游戏编号(gamecode)
  13. * $orderArr['game_order_id'] = 'game_order_id';//游戏订单id
  14. * $orderArr['product_id'] = 'product_id';//商品ID
  15. * $orderArr['product_name'] = 'product_name';//商品名称
  16. * $orderArr['product_des'] = 'product_des';//商品描述
  17. * $orderArr['amount'] = 'amount';//金额,单位为分
  18. * $orderArr['notify_url'] = 'notify_url';//支付回调地址
  19. * $orderArr['user_code'] = 'user_code';//用户编号
  20. * $orderArr['session_id'] = 'session_id';
  21. * $orderArr['role_name'] = 'role_name';// 游戏角色名
  22. * $orderArr['role_level'] = 'role_level';// 游戏角色等级
  23. * $orderArr['area_server'] = 'area_server';// 游戏区服名称
  24. * $orderArr['vip_level'] = 'vip_level';//游戏vip等级
  25. * $orderArr['role_id'] = 'role_id';// 游戏角色ID
  26. * $orderArr['area_server_id'] = 'area_server_id';// 游戏区服ID
  27. * $orderArr['channel_reserved '] = 'channel_reserved ';//cp自定义字段返回
  28. * 聚乐订单签名
  29. */
  30. // 注意:参数里,不要出现类似“1元=10000个金币”的字段,因为“=”原因,会导致微信支付校验失败
  31. // 参数值不能出现json串,会导致支付宝扫码支付失败
  32. // product_id 为字符串类型
  33. function order_rsa_sign($orderArr, $private_key)
  34. {
  35. if (!is_array($orderArr)) {
  36. return false;
  37. }
  38. $pem = chunk_split($private_key, 64, "\n");
  39. $pem = "-----BEGIN PRIVATE KEY-----\n" . $pem . "-----END PRIVATE KEY-----\n";
  40. $res = openssl_get_privatekey($pem);
  41. if (!$res) {
  42. return false;
  43. }
  44. //参数 JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES $orderArr转JSON格式时中文和斜杠不转义
  45. openssl_sign(json_encode($orderArr,JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES), $sign, $res);
  46. openssl_free_key($res);
  47. $sign = base64_encode($sign);
  48. return $sign;
  49. }
  50. function verification_sign($input, $sign, $pub)
  51. {
  52. if (empty($input) || empty($sign) || empty($pub)) {
  53. return false;
  54. }
  55. $pub_key = $this->setupPubKey($pub);
  56. $res = openssl_get_publickey($pub_key);
  57. $result = openssl_verify($input, base64_decode($sign), $res);
  58. if ($res) {
  59. openssl_free_key($res);
  60. }
  61. return $result == 1 ? true : false;
  62. }
  63. function setupPubKey($pubKey)
  64. {
  65. if (is_resource($pubKey)) {
  66. return true;
  67. }
  68. $pem = chunk_split($pubKey, 64, "\n");
  69. $pem = "-----BEGIN PUBLIC KEY-----\n" . $pem . "-----END PUBLIC KEY-----\n";
  70. $pub_Key = openssl_pkey_get_public($pem);
  71. return $pub_Key;
  72. }
  73. function rsa_verify($data, $signature, $public_key) {
  74. if(empty($data) || empty($signature)) {
  75. return false;
  76. }
  77. $pub_res = openssl_get_publickey($public_key);
  78. return openssl_verify($data, base64_decode($signature), $pub_res );
  79. }
  80. }