Skip to content

Instantly share code, notes, and snippets.

@drobinson
Last active August 25, 2020 23:06
Show Gist options
  • Save drobinson/1424ff46f177d4c05ad1cbe518dee653 to your computer and use it in GitHub Desktop.
Save drobinson/1424ff46f177d4c05ad1cbe518dee653 to your computer and use it in GitHub Desktop.
Quick PHP implementation of AWS Signature Version 4 docs example request
<?php
// See docs here: https://docs.aws.amazon.com/general/latest/gr/sigv4_signing.html
// running example here: https://3v4l.org/E3oBG
$body = '';
$bodyHash = hash('sha256', $body);
$canonicalRequest = <<<HEREDOC
GET
/
Action=ListUsers&Version=2010-05-08
content-type:application/x-www-form-urlencoded; charset=utf-8
host:iam.amazonaws.com
x-amz-date:20150830T123600Z
content-type;host;x-amz-date
$bodyHash
HEREDOC;
$canonicalRequestHash = hash('sha256', $canonicalRequest);
$stringToSign = <<<HEREDOC
AWS4-HMAC-SHA256
20150830T123600Z
20150830/us-east-1/iam/aws4_request
$canonicalRequestHash
HEREDOC;
$region = 'us-east-1';
$service = 'iam';
$date = '20150830';
$kSecret = 'wJalrXUtnFEMI/K7MDENG+bPxRfiCYEXAMPLEKEY';
$kDate = hash_hmac('sha256', $date, 'AWS4' . $kSecret, TRUE);
$kRegion = hash_hmac('sha256', $region, $kDate, TRUE);
$kService = hash_hmac('sha256', $service, $kRegion, TRUE);
$kSigning = hash_hmac('sha256', "aws4_request", $kService, TRUE);
$signature = hash_hmac('sha256', $stringToSign, $kSigning);
echo $signature;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment