为什么 HMAC sha256 return 在 PHP 和 Javascript 上的值不同
Why HMAC sha256 return different value on PHP & Javascript
我正在尝试使用 CryptoJS 在 Javascript 中构建 HMAC SHA256 字符串,我现有的代码是使用 Akamai 库在 PHP 中编写的。
在某些情况下,我得到的结果与 PHP 不同,而且我无法理解为什么它会给我不同的结果
/*
<php> Using native hash_hmac
Generating key by concatenating char
*/
$signature1 = hash_hmac('SHA256', "st=1453362060~exp=1453363260~acl=/*", chr(63));
$signature2 = hash_hmac('SHA256', "st=1453362060~exp=1453363260~acl=/*", chr(63) . chr(23));
$signature3 = hash_hmac('SHA256', "st=1453362060~exp=1453363260~acl=/*", chr(63) . chr(23) . chr(253));
/*
here is result from php
signature1 : 3e086bb48ab9aafa85661f9ce1b7dac49befddf117ce2a42d93c92b6abe513ce ( matched: same as JavaScript)
signature2 : 3667dd414a50f68f7ce083e540f27f68f7d0f18617b1fb1e4788bffeaeab59f6( matched: same as JavaScript)
signature3 : dd5a20041661046fdee871c8b9e77b3190fbbf85937c098090a1d524719b6aa9 ( not matched: diff from JavaScript)
*/
/*
<JavaScript> using CryptoJS
Generating key by concatenating three char
*/
var signature1 = CryptoJS.HmacSHA256("st=1453362060~exp=1453363260~acl=/*", String.fromCharCode(63));
var signature2 = CryptoJS.HmacSHA256("st=1453362060~exp=1453363260~acl=/*", String.fromCharCode(63) + String.fromCharCode(23));
var signature3 = CryptoJS.HmacSHA256("st=1453362060~exp=1453363260~acl=/*", String.fromCharCode(63) + String.fromCharCode(23) + String.fromCharCode(253));
/*
here is result from JavaScript
signature1 : 3e086bb48ab9aafa85661f9ce1b7dac49befddf117ce2a42d93c92b6abe513ce ( matched: same as php)
signature2 : 3667dd414a50f68f7ce083e540f27f68f7d0f18617b1fb1e4788bffeaeab59f6 ( matched: same as php)
signature3 : 28075dc75de9f22f83e87772f09a89efb007f2e298167686832eff122ef6eb08 ( not matched: diff from php)
*/
前两个 HMAC 值匹配但是当我附加第三个字符时它产生不同的结果,任何人都可以解释这是为什么。
CryptoJS 在创建哈希 sha256 时在 "Key" 中添加 UTF8 编码,以便我们获得不同的值。
如果我将 utf8_encode 包装在 PHP 一侧,那么我们将获得与 JavaScript
相同的 hmac 值
// <php>
$key = chr(63) . chr(23) . chr(253);
signature3 = hash_hmac('SHA256', "st=1453362060~exp=1453363260~acl=/*", utf8_encode($key));
我正在尝试使用 CryptoJS 在 Javascript 中构建 HMAC SHA256 字符串,我现有的代码是使用 Akamai 库在 PHP 中编写的。
在某些情况下,我得到的结果与 PHP 不同,而且我无法理解为什么它会给我不同的结果
/*
<php> Using native hash_hmac
Generating key by concatenating char
*/
$signature1 = hash_hmac('SHA256', "st=1453362060~exp=1453363260~acl=/*", chr(63));
$signature2 = hash_hmac('SHA256', "st=1453362060~exp=1453363260~acl=/*", chr(63) . chr(23));
$signature3 = hash_hmac('SHA256', "st=1453362060~exp=1453363260~acl=/*", chr(63) . chr(23) . chr(253));
/*
here is result from php
signature1 : 3e086bb48ab9aafa85661f9ce1b7dac49befddf117ce2a42d93c92b6abe513ce ( matched: same as JavaScript)
signature2 : 3667dd414a50f68f7ce083e540f27f68f7d0f18617b1fb1e4788bffeaeab59f6( matched: same as JavaScript)
signature3 : dd5a20041661046fdee871c8b9e77b3190fbbf85937c098090a1d524719b6aa9 ( not matched: diff from JavaScript)
*/
/*
<JavaScript> using CryptoJS
Generating key by concatenating three char
*/
var signature1 = CryptoJS.HmacSHA256("st=1453362060~exp=1453363260~acl=/*", String.fromCharCode(63));
var signature2 = CryptoJS.HmacSHA256("st=1453362060~exp=1453363260~acl=/*", String.fromCharCode(63) + String.fromCharCode(23));
var signature3 = CryptoJS.HmacSHA256("st=1453362060~exp=1453363260~acl=/*", String.fromCharCode(63) + String.fromCharCode(23) + String.fromCharCode(253));
/*
here is result from JavaScript
signature1 : 3e086bb48ab9aafa85661f9ce1b7dac49befddf117ce2a42d93c92b6abe513ce ( matched: same as php)
signature2 : 3667dd414a50f68f7ce083e540f27f68f7d0f18617b1fb1e4788bffeaeab59f6 ( matched: same as php)
signature3 : 28075dc75de9f22f83e87772f09a89efb007f2e298167686832eff122ef6eb08 ( not matched: diff from php)
*/
前两个 HMAC 值匹配但是当我附加第三个字符时它产生不同的结果,任何人都可以解释这是为什么。
CryptoJS 在创建哈希 sha256 时在 "Key" 中添加 UTF8 编码,以便我们获得不同的值。
如果我将 utf8_encode 包装在 PHP 一侧,那么我们将获得与 JavaScript
相同的 hmac 值 // <php>
$key = chr(63) . chr(23) . chr(253);
signature3 = hash_hmac('SHA256', "st=1453362060~exp=1453363260~acl=/*", utf8_encode($key));