Sign.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. <?php namespace Api\Lib\Platform;
  2. use Dever;
  3. class Sign
  4. {
  5. public function __construct($field, $config)
  6. {
  7. $this->field = $field;
  8. $this->config = $config;
  9. if (isset($this->config['sort']) && $this->config['sort'] == 3) {
  10. ksort($this->field->body);
  11. }
  12. }
  13. public function get()
  14. {
  15. if ($this->config['method'] == -1) {
  16. return false;
  17. }
  18. $this->create();
  19. if ($this->config['sort'] == 2) {
  20. ksort($this->info);
  21. }
  22. $this->split();
  23. $this->toString();
  24. $this->encrypt();
  25. $this->after();
  26. $this->field->setSign($this->info);
  27. return $this->info;
  28. }
  29. public function check($sign)
  30. {
  31. if ($this->config['method'] == -1) {
  32. return false;
  33. }
  34. if ($this->config['verify_type'] == 2 && $this->config['method'] > 0) {
  35. $this->create();
  36. if ($this->config['sort'] == 2) {
  37. ksort($this->info);
  38. }
  39. $this->split();
  40. $this->toString();
  41. $check = $this->field->ssl->decrypt($this->config['method'], $sign, $this->info);
  42. } else {
  43. $check = $sign == $this->get();
  44. }
  45. if (!$check) {
  46. Dever::error('签名验证失败');
  47. }
  48. }
  49. protected function create()
  50. {
  51. $this->info = array();
  52. if ($this->config['col']) {
  53. $col = explode('+', $this->config['col']);
  54. foreach ($col as $k => $v) {
  55. if ($v == 'body') {
  56. $this->info = array_merge($this->field->body, $this->info);
  57. } else {
  58. $k = $v;
  59. if (strstr($v, '=')) {
  60. $t = explode('=', $v);
  61. $v = $t[1];
  62. $k = $t[0];
  63. }
  64. $this->info[$k] = $this->field->value($v);
  65. }
  66. }
  67. } else {
  68. $this->info = $this->field->body;
  69. }
  70. }
  71. protected function split()
  72. {
  73. if (strstr($this->config['split'], '\\')) {
  74. $this->config['split'] = preg_replace_callback(
  75. '/\\\\([nrtf])/', // 匹配 \n, \r, \t, \f 等特殊字符
  76. function ($matches) {
  77. $map = [
  78. 'n' => "\n",
  79. 'r' => "\r",
  80. 't' => "\t",
  81. 'f' => "\f"
  82. ];
  83. return $map[$matches[1]]; // 直接从映射中获取替换值
  84. },
  85. $this->config['split']
  86. );
  87. }
  88. }
  89. protected function toString()
  90. {
  91. $string = '';
  92. foreach ($this->info as $k => $v) {
  93. if ($this->config['empty'] == 2 && !$v) {
  94. continue;
  95. }
  96. if ($this->config['encode'] == 2 && strstr($v, 'http')) {
  97. $v = urlencode($v);
  98. if (isset($this->field->body[$k])) {
  99. $this->field->body[$k] = $v;
  100. }
  101. }
  102. if ($this->config['type'] == 1) {
  103. $string .= $v;
  104. } elseif ($this->config['type'] == 2) {
  105. $string .= $k . '=' . $v;
  106. } elseif ($this->config['type'] == 3) {
  107. $string .= $k . $v;
  108. }
  109. $string .= "{$this->config['split']}";
  110. }
  111. if ($this->config['split_type'] == 1) {
  112. $this->info = rtrim($string, $this->config['split']);
  113. } else {
  114. $this->info = $string;
  115. }
  116. }
  117. protected function encrypt()
  118. {
  119. if ($this->config['method'] == -2) {
  120. $this->info = md5($this->info);
  121. } elseif ($this->config['method'] == -3) {
  122. $this->info = hash("sha256", $this->info);
  123. } elseif ($this->config['method'] == -4) {
  124. $this->info = sha1($this->info);
  125. } else {
  126. $this->info = $this->field->ssl->encrypt($this->config['method'], $this->info);
  127. }
  128. }
  129. protected function after()
  130. {
  131. if ($this->config['after'] == 2) {
  132. $this->info = strtoupper($this->info);
  133. } elseif ($this->config['after'] == 2) {
  134. $this->info = strtolower($this->info);
  135. }
  136. }
  137. }