| 1 |
<?php |
| 2 |
|
| 3 |
namespace Dudlewebs\WPMCS\s3\Aws\Signature; |
| 4 |
|
| 5 |
/** |
| 6 |
* Provides signature calculation for SignatureV4. |
| 7 |
*/ |
| 8 |
trait SignatureTrait |
| 9 |
{ |
| 10 |
/** @var array Cache of previously signed values */ |
| 11 |
private $cache = []; |
| 12 |
/** @var int Size of the hash cache */ |
| 13 |
private $cacheSize = 0; |
| 14 |
private function createScope($shortDate, $region, $service) |
| 15 |
{ |
| 16 |
return "{$shortDate}/{$region}/{$service}/aws4_request"; |
| 17 |
} |
| 18 |
private function getSigningKey($shortDate, $region, $service, $secretKey) |
| 19 |
{ |
| 20 |
$k = $shortDate . '_' . $region . '_' . $service . '_' . $secretKey; |
| 21 |
if (!isset($this->cache[$k])) { |
| 22 |
// Clear the cache when it reaches 50 entries |
| 23 |
if (++$this->cacheSize > 50) { |
| 24 |
$this->cache = []; |
| 25 |
$this->cacheSize = 0; |
| 26 |
} |
| 27 |
$dateKey = \hash_hmac('sha256', $shortDate, "AWS4{$secretKey}", \true); |
| 28 |
$regionKey = \hash_hmac('sha256', $region, $dateKey, \true); |
| 29 |
$serviceKey = \hash_hmac('sha256', $service, $regionKey, \true); |
| 30 |
$this->cache[$k] = \hash_hmac('sha256', 'aws4_request', $serviceKey, \true); |
| 31 |
} |
| 32 |
return $this->cache[$k]; |
| 33 |
} |
| 34 |
} |
| 35 |
|