Auth.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <?php
  2. namespace Qcloud_cos;
  3. class Auth
  4. {
  5. const AUTH_URL_FORMAT_ERROR = -1;
  6. const AUTH_SECRET_ID_KEY_ERROR = -2;
  7. /**
  8. * 生成多次有效签名函数(用于上传和下载资源,有效期内可重复对不同资源使用)
  9. * @param int $expired 过期时间,unix时间戳
  10. * @param string $bucketName 文件所在bucket
  11. * @return string 签名
  12. */
  13. public static function appSign($expired, $bucketName) {
  14. $appId = Conf::APPID;
  15. $secretId = Conf::SECRET_ID;
  16. $secretKey = Conf::SECRET_KEY;
  17. if (empty($secretId) || empty($secretKey) || empty($appId)) {
  18. return self::AUTH_SECRET_ID_KEY_ERROR;
  19. }
  20. return self::appSignBase($appId, $secretId, $secretKey, $expired, null, $bucketName);
  21. }
  22. /**
  23. * 生成单次有效签名函数(用于删除和更新指定fileId资源,使用一次即失效)
  24. * @param string $fileId 文件路径,以 /{$appId}/{$bucketName} 开头
  25. * @param string $bucketName 文件所在bucket
  26. * @return string 签名
  27. */
  28. public static function appSign_once($path, $bucketName) {
  29. $appId = Conf::APPID;
  30. $secretId = Conf::SECRET_ID;
  31. $secretKey = Conf::SECRET_KEY;
  32. if (preg_match('/^\//', $path) == 0) {
  33. $path = '/' . $path;
  34. }
  35. $fileId = '/' . $appId . '/' . $bucketName . $path;
  36. if (empty($secretId) || empty($secretKey) || empty($appId)) {
  37. return self::AUTH_SECRET_ID_KEY_ERROR;
  38. }
  39. return self::appSignBase($appId, $secretId, $secretKey, 0, $fileId, $bucketName);
  40. }
  41. /**
  42. * 生成绑定资源的多次有效签名
  43. * @param string $path 文件相对bucket的路径 /test/test.log 标识该bucket下test目录下的test.log文件
  44. * @param string $bucketName bucket
  45. * @param int $expired 过期时间,unix时间戳
  46. * @return string 签名串
  47. */
  48. public static function appSign_multiple($path, $bucketName, $expired) {
  49. $appId = Conf::APPID;
  50. $secretId = Conf::SECRET_ID;
  51. $secretKey = Conf::SECRET_KEY;
  52. if (preg_match('/^\//', $path) == 0) {
  53. $path = '/' . $path;
  54. }
  55. $fileId = $path;
  56. if (empty($secretId) || empty($secretKey) || empty($appId)) {
  57. return self::AUTH_SECRET_ID_KEY_ERROR;
  58. }
  59. return self::appSignBase($appId, $secretId, $secretKey, $expired, $fileId, $bucketName);
  60. }
  61. /**
  62. * 签名函数(上传、下载会生成多次有效签名,删除资源会生成单次有效签名)
  63. * @param string $appId
  64. * @param string $secretId
  65. * @param string $secretKey
  66. * @param int $expired 过期时间,unix时间戳
  67. * @param string $fileId 文件路径,以 /{$appId}/{$bucketName} 开头
  68. * @param string $bucketName 文件所在bucket
  69. * @return string 签名
  70. */
  71. private static function appSignBase($appId, $secretId, $secretKey, $expired, $fileId, $bucketName) {
  72. if (empty($secretId) || empty($secretKey)) {
  73. return self::AUTH_SECRET_ID_KEY_ERROR;
  74. }
  75. $now = time();
  76. $rdm = rand();
  77. $plainText = "a=$appId&k=$secretId&e=$expired&t=$now&r=$rdm&f=$fileId&b=$bucketName";
  78. $bin = hash_hmac('SHA1', $plainText, $secretKey, true);
  79. $bin = $bin.$plainText;
  80. $sign = base64_encode($bin);
  81. return $sign;
  82. }
  83. }
  84. //end of script