[英]PHP Generate HMAC-SHA1 signature
This is driving me crazy. 这真让我抓狂。
I am trying to generate a signature as suggested here: https://www.reed.co.uk/developers/SignatureTest 我正在尝试按照此处的建议生成签名: https : //www.reed.co.uk/developers/SignatureTest
This way: 这条路:
function createSignature($queryUrl, $timestamp, $apiKey, $http = "GET", $agent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0)"){
$signature = $http . $agent . $queryUrl . "www.reed.co.uk" . $timestamp;
$signature = base64_encode(hash_hmac("sha1", $signature, $apiKey, true));
return $signature;
}
$clientId = 1;
$timestamp = "2016-05-13T09:22:50Z";
$apiKey = "bacd2d2d-8b69-43c8-94c5-4a24c0269b79";
$queryUrl = "https://www.reed.co.uk/recruiter/api/1.0/cvsearch";
$reedQuery = \Httpful\Request::get($queryUrl)
->addHeaders(array(
"X-ApiSignature" => createSignature($queryUrl, $timestamp, $apiKey),
"X-ApiClientId" => $clientId,
"X-TimeStamp" => $timestamp
))
->expectsJson()
->send();
print_r($reedQuery);
Now for some reasons it returns this on my server: WRTjqQKfyEQyLJEzWWuT3SWgGPk=
While the expected result is: JUgvCh5oeFYe1HDmfiMObOu1+nQ=
现在由于某些原因它在我的服务器上返回:
WRTjqQKfyEQyLJEzWWuT3SWgGPk=
虽然预期的结果是: JUgvCh5oeFYe1HDmfiMObOu1+nQ=
I tried everything, even swapping from little endian to big endian. 我尝试了一切,甚至从小端到大端交换。 Nothing.
没有。
What is wrong??? 怎么了??? :(
:(
I struggled with the same issue; 我在同样的问题上挣扎; the solution is posted here under a different question.
该解决方案在此处根据不同的问题发布。
The issue is that the string key (GUID) needs its order of the 2-character hexadecimal numbers reversed in the first 3 segments ( http://msdn.microsoft.com/en-us/library/system.guid.tobytearray.aspx ). 问题是字符串键(GUID)需要在前3个段中反转的2个字符的十六进制数字的顺序( http://msdn.microsoft.com/en-us/library/system.guid.tobytearray.aspx )。
Example to create the hash (using your key above): 创建哈希的示例(使用上面的密钥):
$apiToken = 'bacd2d2d-8b69-43c8-94c5-4a24c0269b79';
$stringToSign = 'POSTReedAgenthttps://www.reed.co.uk/recruiter/api/1.0/jobswww.reed.co.uk2017-11-11T13:50:06+00:00';
$hexStr = str_replace('-','',$apiToken);
$c = explode('-',chunk_split($hexStr,2,'-'));
$hexArr = array($c[3],$c[2],$c[1],$c[0],$c[5],$c[4],$c[7],$c[6],$c[8],$c[9],$c[10],$c[11],$c[12],$c[13],$c[14],$c[15]);
$keyStr = '';
for ($i = 0; $i < 16; ++$i) {
$num = hexdec($hexArr[$i]);
$keyStr .= chr($num);
}
$apiSignature = base64_encode(hash_hmac('sha1',$stringToSign,$keyStr,true));
This produces a hash that matches that from the Signature Test . 这会产生与签名测试匹配的哈希。
对于sha1,只需要hash_hmac函数
hash_hmac('sha1', $inputText, $keyString)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.