Rsaauth.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <?php
  2. /**
  3. * Rsaauth.class.php UTF-8
  4. * 对称 非对称加密 请求与返回
  5. *
  6. * @date : 2016年11月9日下午11:46:45
  7. *
  8. * @license 这不是一个自由软件,未经授权不许任何使用和传播。
  9. * @author : wuyonghong <wyh@huosdk.com>
  10. * @version : HUOSDK 7.0
  11. * @modified: 2016年11月9日下午11:46:45
  12. */
  13. namespace huosdk\response;
  14. use think\Log;
  15. use think\Loader;
  16. use huosdk\common\Rsa;
  17. use huosdk\common\Authcode;
  18. class Rsaauth {
  19. private $rsa_pri_path = null;
  20. private $limit_time_diff = 5;
  21. private $time_flag = true;
  22. /**
  23. * 自定义错误处理
  24. *
  25. * @param msg 输出的文件
  26. */
  27. private function _error($msg, $level = 'error') {
  28. $_info = 'Rsaauth Error:'.$msg;
  29. Log::record($_info, 'error');
  30. }
  31. /**
  32. * Rsaauth constructor.
  33. *
  34. * @param bool $time_flag 时间标记
  35. * @param int $limit_time_diff
  36. * @param string $rsa_pri_path
  37. */
  38. public function __construct($time_flag = true, $limit_time_diff = 5, $rsa_pri_path = '') {
  39. $this->time_flag = $time_flag;
  40. $this->limit_time_diff = $limit_time_diff;
  41. $this->rsa_pri_path = $rsa_pri_path;
  42. }
  43. public function getAuthkey($key = '', $change_flag = true, $rsa_pri_path = '') {
  44. $_pri_path = $rsa_pri_path;
  45. if (empty($_pri_path)) {
  46. $_pri_path = $this->rsa_pri_path;
  47. }
  48. $_rsa_class = new \huosdk\common\Rsa('', $_pri_path);
  49. $_key = $key;
  50. if (empty($_key) && !empty($_POST['key'])) {
  51. $_key = urldecode($_POST['key']);
  52. }
  53. $_rsa_key = $_rsa_class->decrypt($_key);
  54. if (!$_rsa_key) {
  55. return false;
  56. }
  57. $_rsa_key_arr = explode('_', $_rsa_key);
  58. $_client_id = $_rsa_key_arr[0];
  59. $_time = $_rsa_key_arr[1];
  60. $_rand16 = $_rsa_key_arr[2];
  61. if (empty($_client_id) || empty($_time) || empty($_rand16)) {
  62. return false;
  63. }
  64. if ($change_flag) {
  65. $_game_class = new \huosdk\game\Game(0, $_client_id);
  66. $_client_key = $_game_class->getClientkey($_client_id);
  67. if (empty($_client_key)) {
  68. return false;
  69. }
  70. $_auth_key = $_client_key.$_rand16;
  71. } else {
  72. $_auth_key = $_rsa_key;
  73. }
  74. // $_time_diff = $this->timeDiff($_time);
  75. // if ($this->time_flag && $_time > $this->limit_time_diff){
  76. // return false;
  77. // }
  78. return $_auth_key;
  79. }
  80. public function timeDiff($time) {
  81. $_now_time = time();
  82. $_time_diff = abs($_now_time - $time);
  83. return $_time_diff;
  84. }
  85. /**
  86. * 获取请求数据
  87. *
  88. * @param $path 文件创建路径
  89. * @param $name 文件创建名称
  90. *
  91. * @return bool 成功返回true 失败返回 false
  92. */
  93. public function getRqdata($key, $data = '') {
  94. $_data = $data;
  95. if (empty($_data) && !empty($_POST['data'])) {
  96. $_data = urldecode($_POST['data']);
  97. }
  98. $_ac_class = new \huosdk\common\Authcode();
  99. $_rq_data = $_ac_class->discuzAuthcode($_data, 'DECODE', $key);
  100. if (empty($_rq_data)) {
  101. return false;
  102. }
  103. $_rq_data = json_decode($_rq_data, true);
  104. return $_rq_data;
  105. }
  106. public function getAuthdata(array $responcedata, $key) {
  107. $_authdata['responcedata'] = json_encode($responcedata);
  108. $_rsa_class = new \huosdk\common\Rsa('', $this->rsa_pri_path);
  109. $_authdata['sign'] = $_rsa_class->sign($_authdata['responcedata']);
  110. //对称加密
  111. $_auth_class = new \huosdk\common\authCode();
  112. $_auth_jsondata = json_encode($_authdata);
  113. return $_auth_class->discuzAuthcode($_auth_jsondata, 'ENCODE', $key, 0);
  114. }
  115. }