我遇到了hash_hmac和AWS签名版本4的问题。我正在使用他们在此处列出的示例:http://docs.aws.amazon.com/general/latest/gr/sigv4-calculate-signature.html
输出来自AWS网站。我想要匹配它,我似乎无法看到我做错了什么。他们想要二进制输出,这就是我在每一步中提供的。
这是我的测试文件:
<?php
// wJalrXUtnFEMI/K7MDENG+bPxRfiCYEXAMPLEKEY
// HMAC(HMAC(HMAC(HMAC("AWS4" + kSecret,"20110909"),"us-east-1"),"iam"),"aws4_request")
$sign = hash_hmac('sha256', 'AWS4wJalrXUtnFEMI/K7MDENG+bPxRfiCYEXAMPLEKEY', '20110909', true );
$sign = hash_hmac('sha256', $sign, 'us-east-1', true );
$sign = hash_hmac('sha256', $sign, 'iam', true );
$sign = hash_hmac('sha256', $sign, 'aws4_request', true );
$sign = str_split( $sign );
echo "152 241 216 137 254 196 244 66 26 220 82 43 171 12 225 248 46 105 41 194 98 237 21 229 169 76 144 239 209 227 176 231\n";
foreach( $sign as $t )
echo ord($t) . ' ';
答案 0 :(得分:2)
正确的格式是相反的:
<?php
// wJalrXUtnFEMI/K7MDENG+bPxRfiCYEXAMPLEKEY
// HMAC(HMAC(HMAC(HMAC("AWS4" + kSecret,"20110909"),"us-east-1"),"iam"),"aws4_request")
$sign = hash_hmac('sha256', '20110909', 'AWS4wJalrXUtnFEMI/K7MDENG+bPxRfiCYEXAMPLEKEY', true );
$sign = hash_hmac('sha256', 'us-east-1', $sign, true );
$sign = hash_hmac('sha256', 'iam', $sign, true );
$sign = hash_hmac('sha256', 'aws4_request', $sign, true );
$sign = str_split( $sign );
echo "152 241 216 137 254 196 244 66 26 220 82 43 171 12 225 248 46 105 41 194 98 237 21 229 169 76 144 239 209 227 176 231\n";
foreach( $sign as $t )
echo ord($t) . ' ';
这很好地匹配给定的字符串。希望这有助于某人!