BsPayTools.php 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <?php
  2. namespace BsPaySdk\core;
  3. class BsPayTools
  4. {
  5. /**
  6. * 私钥加签(对数据源排序),可用于 V2 版本接口数据加签
  7. *
  8. * @param @param array $data 原数据( 排序后的json字符串; 数组参数排序后转json字符串(数据的中文和斜杠均不转码):
  9. ksort($post_data); json_encode($post_data, JSON_UNESCAPED_SLASHES|JSON_UNESCAPED_UNICODE); )
  10. * @param string $rsaPrivateKey 私钥
  11. * @param int $alg 默认 OPENSSL_ALGO_SHA256
  12. *
  13. * @return string 签名串
  14. */
  15. public static function sha_with_rsa_sign($data, $rsaPrivateKey, $alg=OPENSSL_ALGO_SHA256){
  16. $key = "-----BEGIN PRIVATE KEY-----\n".wordwrap($rsaPrivateKey, 64, "\n", true)."\n-----END PRIVATE KEY-----";
  17. $signature= '';
  18. try {
  19. openssl_sign($data, $signature, $key, $alg);
  20. } catch (\Exception $e) {
  21. echo $e->getMessage();
  22. }
  23. return base64_encode($signature);
  24. }
  25. public static function encrypt_with_rsa_pubkey($data, $rsaPublicKey, $padding=OPENSSL_PKCS1_PADDING){
  26. $key = "-----BEGIN PUBLIC KEY-----\n".wordwrap($rsaPublicKey, 64, "\n", true)."\n-----END PUBLIC KEY-----";
  27. $encryptResult= '';
  28. try {
  29. openssl_public_encrypt($data, $encryptResult, $key, $padding);
  30. } catch (\Exception $e) {
  31. echo $e->getMessage();
  32. }
  33. return base64_encode($encryptResult);
  34. }
  35. /**
  36. * 使用公钥验签,可用于异步应答验签
  37. *
  38. * @param string $signature 签文
  39. * @param string $data 原数据(string)
  40. * @param string $rsaPublicKey 公钥
  41. * @param int $alg 默认 OPENSSL_ALGO_SHA256
  42. *
  43. * @return false|int 验证结果:成功/失败
  44. */
  45. public static function verifySign($signature, $data, $rsaPublicKey, $alg=OPENSSL_ALGO_SHA256){
  46. $key = "-----BEGIN PUBLIC KEY-----\n".wordwrap($rsaPublicKey, 64, "\n", true)."\n-----END PUBLIC KEY-----";
  47. return openssl_verify($data, base64_decode($signature), $key, $alg);
  48. }
  49. /**
  50. * 使用公钥验签(对数据源排序),可用于 V2 版本接口返回数据验签
  51. *
  52. * @param string $signature 签文
  53. * @param array $data 原数据(array)
  54. * @param string $rsaPublicKey 公钥
  55. * @param int $alg 默认 OPENSSL_ALGO_SHA256
  56. *
  57. * @return false|int 验证结果:成功/失败
  58. */
  59. public static function verifySign_sort($signature, $data, $rsaPublicKey, $alg=OPENSSL_ALGO_SHA256){
  60. $key = "-----BEGIN PUBLIC KEY-----\n".wordwrap($rsaPublicKey, 64, "\n", true)."\n-----END PUBLIC KEY-----";
  61. ksort($data);
  62. return openssl_verify(json_encode($data, JSON_UNESCAPED_SLASHES|JSON_UNESCAPED_UNICODE), base64_decode($signature), $key, $alg);
  63. }
  64. public static function checkEmpty($value) {
  65. return !isset($value) || trim($value) === "";
  66. }
  67. public static function endWith($str, $suffix) {
  68. $length = strlen($suffix);
  69. if($length == 0){
  70. return false;
  71. }
  72. return (substr($str, -$length) === $suffix);
  73. }
  74. /**
  75. * 校验 webhook 返回报文签名
  76. *
  77. * @param string $signature 签文
  78. * @param array $data 原数据(array)
  79. * @param string $key 加签 key
  80. * @param int $alg 默认 OPENSSL_ALGO_SHA256
  81. *
  82. * @return true|false 验证结果:成功/失败
  83. */
  84. public static function verify_webhook_sign($signature, $data, $key){
  85. $sign = md5(json_encode($data, JSON_UNESCAPED_SLASHES|JSON_UNESCAPED_UNICODE).$key);
  86. return $sign == strtolower($signature);
  87. }
  88. }