PHP和Paw REST客户端之间的哈希不相似
我正在构建一个HMACAPI,在使用Paw测试哈希时遇到了问题 在Paw上,我有一个有效载荷:PHP和Paw REST客户端之间的哈希不相似,php,hmac,sha256,paw-app,Php,Hmac,Sha256,Paw App,我正在构建一个HMACAPI,在使用Paw测试哈希时遇到了问题 在Paw上,我有一个有效载荷: GET:/hello/world:"":9a6e30f2016370b6f2dcfb6880501d7f2305d69bout 和一个自定义HMAC-SHA256变量(实际上是在X-Hash头中设置它的函数) X-Hash: 4Cq2yehWumDcUk1dYyfhm6qWjJVBkOCB8o12f5l0WGE= 在我的PHP API中,我有同样的东西: GET:/hello/world:"":9
GET:/hello/world:"":9a6e30f2016370b6f2dcfb6880501d7f2305d69bout
和一个自定义HMAC-SHA256变量(实际上是在X-Hash头中设置它的函数)
X-Hash: 4Cq2yehWumDcUk1dYyfhm6qWjJVBkOCB8o12f5l0WGE=
在我的PHP API中,我有同样的东西:
GET:/hello/world:"":9a6e30f2016370b6f2dcfb6880501d7f2305d69bout
并使用:
hash_hmac('sha256', $this->getPayload(), '9a6e30f2016370b6f2dcfb6880501d7f2305d69bout', false);
因此,在比较哈希时:
Paw: 4Cq2yehWumDcUk1dYyfhm6qWjJVBkOCB8o12f5l0WGE=
PHP: 6961b9d1f6e986c49d963cbebd691fa68dfa59b4ce3b7f05320c2d43eae3c7c3
他们很不一样,知道为什么吗
更新
Paw代码:
function evaluate(context){
var loc = getLocation(context.getCurrentRequest().url);
var payload = "";
payload += context.getCurrentRequest().method + ':';
payload += loc.pathname + ':';
payload += JSON.stringify(context.getCurrentRequest().body) + ':';
payload += "9a6e30f2016370b6f2dcfb6880501d7f2305d69bout"; // Private key
return payload;
};
function getLocation(href) {
var match = href.match(/^(https?\:)\/\/(([^:\/?#]*)(?:\:([0-9]+))?)(\/[^?#]*)(\?[^#]*|)(#.*|)$/);
return match && {
protocol: match[1],
host: match[2],
hostname: match[3],
port: match[4],
pathname: match[5],
search: match[6],
hash: match[7]
}
}
PHP代码(带有大量注释):
希望有帮助!paw哈希是base64编码的,而PHP哈希是十六进制的。因此,请首先解码paw哈希:
$binary = base64_decode($pawHash);
$hex = bin2hex($binary);
然后将其与您自己的哈希进行比较。我们刚刚添加了新的,这应该可以解决您的问题
将HMAC签名动态值包装在新的Base 64 to Hex one中,您将获得一个有效的十六进制签名:
你可以在这里安装这个新的动态值:我不能做相反的事情吗?在发送之前解码Paw中生成的哈希,使我的API保持原样?或者检测它是否是base64编码的?仍然不工作。我按照你说的做了(并检查base64_解码(str,true)!==FALSE)当然这是一个base64字符串,但它现在给了我这个哈希:E02AB6C9E856BA60DC524D65D6327E19BAA968C954190E081F28D767F99745861与我在PHP中的仍然有很大的不同。如果看不到其余的代码,很难判断问题出是什么。例如,负载可能有问题。即使是一个微小的差异,哈希值都是w我看起来完全不一样。把我涂成哑巴,我忘了把“钥匙”字段中的私钥给Paw…aaarg!@this.lau你仍然帮了我很大的忙,所以我会接受你的回答!再次感谢!
public function generateHmac()
{
print 'Generating HMAC' . '<br>';
$algorithm = $this->getAlgorithm();
print 'algo ' . $algorithm . '<br>';
$privateKey = $this->getPrivateKey();
print 'privk ' . $privateKey . '<br>';
if (empty($algorithm)) {
throw new \RuntimeException('Algorithm must be set and not empty');
} elseif (empty($privateKey)) {
throw new \RuntimeException('Private key must be set and not empty');
}
print 'payload ' . $this->getPayload() . '<br>';
$hash = hash_hmac($this->getAlgorithm(), $this->getPayload(), $this->getPrivateKey(), false);
print 'php hasj: ' . $hash . '<br>';
return $hash;
}
$publickey = 95f97b93560f951b4cae46c86d03d9b1a81d4ae8
decoding
$hmacSignature = e02ab6c9e856ba60dc524d5d6327e19baa968c954190e081f28d767f99745861
method = GET
uri = /hello/world
body = ""
private key = 9a6e30f2016370b6f2dcfb6880501d7f2305d69bout
PHP payload [GET:/hello/world:"":9a6e30f2016370b6f2dcfb6880501d7f2305d69bout]
Generating HMAC
algo sha256
privk 9a6e30f2016370b6f2dcfb6880501d7f2305d69bout
payload GET:/hello/world:"":9a6e30f2016370b6f2dcfb6880501d7f2305d69bout
php hash: 6961b9d1f6e986c49d963cbebd691fa68dfa59b4ce3b7f05320c2d43eae3c7c3
$binary = base64_decode($pawHash);
$hex = bin2hex($binary);