Mac.php 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. <?php
  2. namespace Qiniu\Pili;
  3. class Mac
  4. {
  5. public $_accessKey;
  6. public $_secretKey;
  7. public function __construct($accessKey, $secretKey)
  8. {
  9. $this->_accessKey = $accessKey;
  10. $this->_secretKey = $secretKey;
  11. }
  12. public function MACToken($method, $url, $contentType, $body)
  13. {
  14. $url = parse_url($url);
  15. $data = '';
  16. if (!empty($url['path'])) {
  17. $data = $method . ' ' . $url['path'];
  18. }
  19. if (!empty($url['query'])) {
  20. $data .= '?' . $url['query'];
  21. }
  22. if (!empty($url['host'])) {
  23. $data .= "\nHost: " . $url['host'];
  24. if (isset($url['port'])) {
  25. $data .= ':' . $url['port'];
  26. }
  27. }
  28. if (!empty($contentType)) {
  29. $data .= "\nContent-Type: " . $contentType;
  30. }
  31. $data .= "\n\n";
  32. if (!empty($body)) {
  33. $data .= $body;
  34. }
  35. return 'Qiniu ' . $this->_accessKey . ':' . Utils::sign($this->_secretKey, $data);
  36. }
  37. }