Aliyun.php 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CatchAdmin [Just Like ~ ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2017~2020 http://catchadmin.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( https://github.com/yanwenwu/catch-admin/blob/master/LICENSE.txt )
  8. // +----------------------------------------------------------------------
  9. // | Author: JaguarJack [ njphper@gmail.com ]
  10. // +----------------------------------------------------------------------
  11. namespace catchAdmin\domain\support\signature;
  12. class Aliyun
  13. {
  14. /**
  15. * @var array
  16. */
  17. protected $params;
  18. /**
  19. * Aliyun constructor.
  20. * @param $params
  21. */
  22. public function __construct(array $params)
  23. {
  24. $this->params = $params;
  25. }
  26. /**
  27. * encode
  28. *
  29. * @time 2020年09月25日
  30. * @param $str
  31. * @return string|string[]|null
  32. */
  33. protected function percentEncode(string $str)
  34. {
  35. return preg_replace(['/\+/', '/\*/', '/%7E/'], ['%20', '%2A', '~'], urlencode($str));
  36. }
  37. /**
  38. * 签名
  39. *
  40. * @time 2020年09月25日
  41. * @param $method
  42. * @return string
  43. */
  44. public function signature(string $method)
  45. {
  46. ksort($this->params);
  47. $queryString = '';
  48. foreach ($this->params as $key => $param) {
  49. $queryString .= '&' . $this->percentEncode($key) . '=' . $this->percentEncode($param);
  50. }
  51. $signString = $method . '&' .
  52. $this->percentEncode('/') . '&' .
  53. $this->percentEncode(substr($queryString, 1));
  54. return base64_encode(hash_hmac('sha1', $signString, config('catch.domains.aliyun.access_secret'). '&', true));
  55. }
  56. }