AWSOAuth.class.php 1.2 KB

123456789101112131415161718192021222324252627282930313233343536
  1. <?php
  2. class AWSOAuth {
  3. private static $SECRET_KEY = "+vB6pF3kIVHOljYeUnjIpziVHcJxy7F9j+fW3eWH";
  4. private static $ACCESS_KEY = "AKIAJHK7XI7GFCS24S2Q";
  5. private STATIC $MINUTES_EXPIRES = 5;
  6. private function lazySignature($key, $data) {
  7. if (strlen($key) > 64) {
  8. $key = pack('H*', sha1($key));
  9. }
  10. $key = str_pad($key, 64, chr(0x00));
  11. $ipad = str_repeat(chr(0x36), 64);
  12. $opad = str_repeat(chr(0x5c), 64);
  13. $hmac = pack( 'H*', sha1(
  14. ($key ^ $opad) . pack( 'H*', sha1(
  15. ($key ^ $ipad) . $data
  16. ))
  17. ));
  18. return base64_encode($hmac);
  19. }
  20. public function getFileSecureParams($bucket, $fileName) {
  21. $expires = time() + intval(floatval(AWSOAuth::$MINUTES_EXPIRES) * 60);
  22. $fileName = str_replace('%2F', '/', rawurlencode($fileName = ltrim($fileName, '/')));
  23. $signpath = '/'. $bucket .'/'. $fileName;
  24. $signsz = implode("\n", $pieces = array('GET', null, null, $expires, $signpath));
  25. $signature = $this->lazySignature(AWSOAuth::$SECRET_KEY, $signsz);
  26. $qs = http_build_query($pieces = array(
  27. 'AWSAccessKeyId' => AWSOAuth::$ACCESS_KEY,
  28. 'Expires' => $expires,
  29. 'Signature' => $signature,
  30. ));
  31. return $qs;
  32. }
  33. }