Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 4 additions & 27 deletions lib/Auth/AWS.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,16 +107,14 @@ public function validate(string $secretKey): bool

$amzHeaders = $this->getAmzHeaders();

$signature = base64_encode(
$this->hmacsha1($secretKey,
$this->request->getMethod()."\n".
$message = $this->request->getMethod()."\n".
$contentMD5."\n".
$this->request->getHeader('Content-type')."\n".
$requestDate."\n".
$amzHeaders.
$this->request->getUrl()
)
);
$this->request->getUrl();

$signature = base64_encode(hash_hmac('sha1', $message, $secretKey, true));

if ($this->signature !== $signature) {
$this->errorCode = self::ERR_INVALIDSIGNATURE;
Expand Down Expand Up @@ -192,25 +190,4 @@ protected function getAmzHeaders(): string

return $headerStr;
}

/**
* Generates an HMAC-SHA1 signature.
*/
private function hmacsha1(string $key, string $message): string
{
if (function_exists('hash_hmac')) {
return hash_hmac('sha1', $message, $key, true);
}

$blocksize = 64;
if (strlen($key) > $blocksize) {
$key = pack('H*', sha1($key));
}
$key = str_pad($key, $blocksize, chr(0x00));
$ipad = str_repeat(chr(0x36), $blocksize);
$opad = str_repeat(chr(0x5C), $blocksize);
$hmac = pack('H*', sha1(($key ^ $opad).pack('H*', sha1(($key ^ $ipad).$message))));

return $hmac;
}
}
21 changes: 1 addition & 20 deletions tests/HTTP/Auth/AWSTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -175,9 +175,7 @@ public function testValidRequest(): void
$date->setTimezone(new \DateTimeZone('GMT'));
$date = $date->format('D, d M Y H:i:s \\G\\M\\T');

$sig = base64_encode($this->hmacsha1($secretKey,
"POST\n$contentMD5\n\n$date\nx-amz-date:$date\n/evert"
));
$sig = base64_encode(hash_hmac('sha1', "POST\n$contentMD5\n\n$date\nx-amz-date:$date\n/evert", $secretKey, true));

$this->request->setUrl('/evert');
$this->request->setMethod('POST');
Expand All @@ -202,21 +200,4 @@ public function test401(): void
$test = preg_match('/^AWS$/', (string) $this->response->getHeader('WWW-Authenticate'), $matches);
self::assertTrue(1 === $test, 'The WWW-Authenticate response didn\'t match our pattern');
}

/**
* Generates an HMAC-SHA1 signature.
*/
private function hmacsha1(string $key, string $message): string
{
$blocksize = 64;
if (strlen($key) > $blocksize) {
$key = pack('H*', sha1($key));
}
$key = str_pad($key, $blocksize, chr(0x00));
$ipad = str_repeat(chr(0x36), $blocksize);
$opad = str_repeat(chr(0x5C), $blocksize);
$hmac = pack('H*', sha1(($key ^ $opad).pack('H*', sha1(($key ^ $ipad).$message))));

return $hmac;
}
}