|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace PhpAwsSmtpPassword; |
| 4 | + |
| 5 | +class PhpAwsSmtpPassword |
| 6 | +{ |
| 7 | + /** |
| 8 | + * Per https://docs.aws.amazon.com/ses/latest/DeveloperGuide/smtp-credentials.html this should never change |
| 9 | + * @var string Date to hash with |
| 10 | + */ |
| 11 | + protected static $date = '11111111'; |
| 12 | + |
| 13 | + /** |
| 14 | + * Per https://docs.aws.amazon.com/ses/latest/DeveloperGuide/smtp-credentials.html this should never change |
| 15 | + * @var string Service to hash with |
| 16 | + */ |
| 17 | + protected static $service = 'ses'; |
| 18 | + |
| 19 | + /** |
| 20 | + * Per https://docs.aws.amazon.com/ses/latest/DeveloperGuide/smtp-credentials.html this should never change |
| 21 | + * @var string Terminal to hash with |
| 22 | + */ |
| 23 | + protected static $terminal = 'aws4_request'; |
| 24 | + |
| 25 | + /** |
| 26 | + * Per https://docs.aws.amazon.com/ses/latest/DeveloperGuide/smtp-credentials.html this should never change |
| 27 | + * @var string Message to hash with |
| 28 | + */ |
| 29 | + protected static $message = 'SendRawEmail'; |
| 30 | + |
| 31 | + /** |
| 32 | + * Per https://docs.aws.amazon.com/ses/latest/DeveloperGuide/smtp-credentials.html this should never change |
| 33 | + * @var string Version to hash with |
| 34 | + */ |
| 35 | + protected static $version = '[4]'; |
| 36 | + |
| 37 | + /** |
| 38 | + * Converts a secret key to a password that can be used with SMTP |
| 39 | + * |
| 40 | + * @param string $key Access secret key |
| 41 | + * @param string $region AWS Region |
| 42 | + * @return string SMTP password |
| 43 | + */ |
| 44 | + public static function convert($key, $region) |
| 45 | + { |
| 46 | + $signature = hash_hmac('sha256', PhpAwsSmtpPassword::$date, 'AWS4' . $key, true); |
| 47 | + $signature = hash_hmac('sha256', $region, $signature, true); |
| 48 | + $signature = hash_hmac('sha256', PhpAwsSmtpPassword::$service, $signature, true); |
| 49 | + $signature = hash_hmac('sha256', PhpAwsSmtpPassword::$terminal, $signature, true); |
| 50 | + $signature = hash_hmac('sha256', PhpAwsSmtpPassword::$message, $signature, true); |
| 51 | + $signatureAndVersion = PhpAwsSmtpPassword::$version . $signature; |
| 52 | + |
| 53 | + return base64_encode($signatureAndVersion); |
| 54 | + } |
| 55 | +} |
0 commit comments