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