Test.php 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. /**
  5. * 手机短信接口
  6. */
  7. class Test extends Api
  8. {
  9. protected $noNeedLogin = '*';
  10. protected $noNeedRight = '*';
  11. //验签
  12. public function verifysign(){
  13. //解密签名
  14. $sign = $this->request->request('sign','','trim');
  15. if(empty($sign)){
  16. $this->error('缺少签名');
  17. }
  18. $sign = base64_decode($sign);
  19. $private_key_str = config('app_rsa.private_key');
  20. $private_key = "-----BEGIN RSA PRIVATE KEY-----" .PHP_EOL.
  21. wordwrap($private_key_str, 64, PHP_EOL, true) .
  22. PHP_EOL."-----END RSA PRIVATE KEY-----";
  23. $signgetdata = ''; //被解密出来的数据
  24. openssl_private_decrypt($sign, $signgetdata, $private_key); // 使用私钥解密数据
  25. if (!$signgetdata) {
  26. $this->error('签名错误');
  27. }
  28. dump($signgetdata);
  29. //接收到的参数
  30. $request_all = $this->request->request();
  31. unset($request_all['s']);
  32. unset($request_all['sign']);
  33. ksort($request_all);
  34. $request_all['signkey'] = 'F_dC923_35270PdsIIUIUTRERYTYYU';
  35. $request_all = json_encode($request_all);
  36. dump($request_all);
  37. //作对比
  38. if($request_all != $signgetdata){
  39. $this->error('验签错误');
  40. }
  41. echo '验签正确';
  42. }
  43. //加密
  44. public function jiami(){
  45. $request_all = $this->request->request();
  46. unset($request_all['s']);
  47. // dump($request_all);
  48. ksort($request_all);
  49. $request_all['signkey'] = 'F_dC923_35270PdsIIUIUTRERYTYYU';
  50. // dump($request_all);
  51. $json_data = json_encode($request_all);
  52. echo $json_data;
  53. $public_key_str = config('app_rsa.public_key');
  54. $public_key = "-----BEGIN PUBLIC KEY-----" .PHP_EOL.
  55. wordwrap($public_key_str, 64, PHP_EOL, true) .
  56. PHP_EOL."-----END PUBLIC KEY-----";
  57. $sign = '';
  58. openssl_public_encrypt($json_data,$sign,$public_key);
  59. $sign = base64_encode($sign);
  60. $this->success('获得签名',$sign);
  61. }
  62. }