123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145 |
- <?php namespace Api\Lib\Platform;
- use Dever;
- class Sign
- {
- public function __construct($field, $config)
- {
- $this->field = $field;
- $this->config = $config;
- if (isset($this->config['sort']) && $this->config['sort'] == 3) {
- ksort($this->field->body);
- }
- }
- public function get()
- {
- if ($this->config['method'] == -1) {
- return false;
- }
- $this->create();
- if ($this->config['sort'] == 2) {
- ksort($this->info);
- }
- $this->split();
- $this->toString();
- $this->encrypt();
- $this->after();
- $this->field->setSign($this->info);
- return $this->info;
- }
- public function check($sign)
- {
- if ($this->config['method'] == -1) {
- return false;
- }
- if ($this->config['verify_type'] == 2 && $this->config['method'] > 0) {
- $this->create();
- if ($this->config['sort'] == 2) {
- ksort($this->info);
- }
- $this->split();
- $this->toString();
- $check = $this->field->ssl->decrypt($this->config['method'], $sign, $this->info);
- } else {
- $check = $sign == $this->get();
- }
- if (!$check) {
- Dever::error('签名验证失败');
- }
- }
- protected function create()
- {
- $this->info = array();
- if ($this->config['col']) {
- $col = explode('+', $this->config['col']);
- foreach ($col as $k => $v) {
- if ($v == 'body') {
- $this->info = array_merge($this->field->body, $this->info);
- } else {
- $k = $v;
- if (strstr($v, '=')) {
- $t = explode('=', $v);
- $v = $t[1];
- $k = $t[0];
- }
- $this->info[$k] = $this->field->value($v);
- }
- }
- } else {
- $this->info = $this->field->body;
- }
- }
- protected function split()
- {
- if (strstr($this->config['split'], '\\')) {
- $this->config['split'] = preg_replace_callback(
- '/\\\\([nrtf])/', // 匹配 \n, \r, \t, \f 等特殊字符
- function ($matches) {
- $map = [
- 'n' => "\n",
- 'r' => "\r",
- 't' => "\t",
- 'f' => "\f"
- ];
- return $map[$matches[1]]; // 直接从映射中获取替换值
- },
- $this->config['split']
- );
- }
- }
- protected function toString()
- {
- $string = '';
- foreach ($this->info as $k => $v) {
- if ($this->config['empty'] == 2 && !$v) {
- continue;
- }
- if ($this->config['encode'] == 2 && strstr($v, 'http')) {
- $v = urlencode($v);
- if (isset($this->field->body[$k])) {
- $this->field->body[$k] = $v;
- }
- }
- if ($this->config['type'] == 1) {
- $string .= $v;
- } elseif ($this->config['type'] == 2) {
- $string .= $k . '=' . $v;
- } elseif ($this->config['type'] == 3) {
- $string .= $k . $v;
- }
-
- $string .= "{$this->config['split']}";
- }
- if ($this->config['split_type'] == 1) {
- $this->info = rtrim($string, $this->config['split']);
- } else {
- $this->info = $string;
- }
- }
- protected function encrypt()
- {
- if ($this->config['method'] == -2) {
- $this->info = md5($this->info);
- } elseif ($this->config['method'] == -3) {
- $this->info = hash("sha256", $this->info);
- } elseif ($this->config['method'] == -4) {
- $this->info = sha1($this->info);
- } else {
- $this->info = $this->field->ssl->encrypt($this->config['method'], $this->info);
- }
- }
- protected function after()
- {
- if ($this->config['after'] == 2) {
- $this->info = strtoupper($this->info);
- } elseif ($this->config['after'] == 2) {
- $this->info = strtolower($this->info);
- }
- }
- }
|