Utils.php 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. <?php
  2. namespace Qiniu\Pili;
  3. final class Utils
  4. {
  5. public static function base64UrlEncode($str)
  6. {
  7. $find = array('+', '/');
  8. $replace = array('-', '_');
  9. return str_replace($find, $replace, base64_encode($str));
  10. }
  11. public static function base64UrlDecode($str)
  12. {
  13. $find = array('-', '_');
  14. $replace = array('+', '/');
  15. return base64_decode(str_replace($find, $replace, $str));
  16. }
  17. public static function digest($secret, $data)
  18. {
  19. return hash_hmac('sha1', $data, $secret, true);
  20. }
  21. public static function sign($secret, $data)
  22. {
  23. return self::base64UrlEncode(self::digest($secret, $data));
  24. }
  25. public static function getUserAgent($sdkName, $sdkVersion)
  26. {
  27. $ua = sprintf("%s/%s", $sdkName, $sdkVersion);
  28. if (extension_loaded('curl')) {
  29. $curlVersion = curl_version();
  30. $ua .= ' curl/' . $curlVersion['version'];
  31. }
  32. $ua .= ' PHP/' . PHP_VERSION;
  33. return $ua;
  34. }
  35. }