简体   繁体   English

使用PHP HMAC SHA1和Base64创建身份验证字符串时遇到问题

[英]Trouble creating auth string using PHP HMAC SHA1 and Base64

So I am working with this API and using Laravel, and I am trying to build an auth string. 因此,我正在使用此API并使用Laravel,并且正在尝试构建auth字符串。 This is the documentation I was given, but I am having a little trouble as this is something relatively new to me. 这是我得到的文档,但是我遇到了一些麻烦,因为这对我来说是相对较新的东西。

Here are the auth instructions: 这是auth指令:

The authentication parameter is a string and it can calculated by the caller or the caller can choose to save this value as a parameter together with connection ID and API key. 身份验证参数是一个字符串,可以由调用方计算出来,或者调用方可以选择将此值与连接ID和API密钥一起保存为参数。

The authentication is a base64 string of a HMAC SHA1 hash. 身份验证是HMAC SHA1哈希的base64字符串。 This is computed by using the binary of API Key in in 这是通过使用

########################## format in all lower case and UTF8 encoding as the key and computer HMAC SHA1 hash on the binary of ##########################格式为小写,以UTF8编码为键,计算机HMAC SHA1散列在

Connection ID in ################################ format in all lower case and UTF8 encoding. 所有小写​​和UTF8编码格式的################################格式的连接ID。

The result binary hash is then base64 encoded and the text result is what should be passed as the authentication parameter. 然后对结果二进制哈希进行base64编码,然后将文本结果作为身份验证参数传递给文本。 In C# the code to calculate the authentication may look like: 在C#中,用于计算身份验证的代码如下所示:

 HMACSHA1 hmac = new HMACSHA1( UTF8Encoding.UTF8.GetBytes(apiKey.ToString("N").ToLower()) ); string authentication = Convert.ToBase64String( hmac.ComputeHash( UTF8Encoding.UTF8.GetBytes(connectionId.ToString("N").ToLower()) ) ); 

As an example the following credentials: 例如,以下凭证:

Connection ID: 5fecbc200f0e4a7cbf41040e11047e56 连接ID: 5fecbc200f0e4a7cbf41040e11047e56

API Key: 2de51c4fd0f04b9fabeb95225e87da70 API密钥: 2de51c4fd0f04b9fabeb95225e87da70

Should result in a computed authentication value of m5/Vc1RzhUETQvEtx/JdIglQpTg= 应导致计算出的认证值m5/Vc1RzhUETQvEtx/JdIglQpTg=

So what i have been trying is: 所以我一直在尝试:

$a = strtolower('5fecbc200f0e4a7cbf41040e11047e56');
$b = strtolower('2de51c4fd0f04b9fabeb95225e87da70');
$z = hash_hmac("sha1", utf8_encode(decbin($b)), utf8_encode(decbin($a)), true);
dd(base64_encode($z));

Which outputs QjG3kzUs7U1UukNd++3t24pBWNk= 输出QjG3kzUs7U1UukNd++3t24pBWNk=

I have tried a few more variations, but I am just lost on this one. 我尝试了更多的变体,但是我只是迷路了。 First time really decoding or encoding anything. 第一次真正解码或编码任何东西。 Would greatly appreciate any tips, ideas, or snippets that can help me figure this out. 非常感谢任何可以帮助我解决此问题的技巧,想法或摘要。 Already spent a few hours on this and it's bugging me.. 已经花了几个小时了,这困扰着我..

First: Get rid of utf8_encode() and just generally don't use it. 首先:摆脱utf8_encode()而通常通常不使用它。 It assumes that the input string is ISO-88591-1 and if it is anything else it will silently corrupt the data. 它假定输入字符串是ISO-88591-1,如果是其他任何字符串,它将无提示地破坏数据。 This function has an incredibly misleading name, and I would go as far as to suggest that no one should ever use it or the corresponding utf8_decode() which will break your data in the same manner, but reversed. 此函数的名称具有令人难以置信的误导性,我甚至建议任何人都不要使用它或相应的utf8_decode() ,它们将以相同的方式破坏您的数据,但相反。

If you need to convert string encodings in PHP use something that explicitly defines the input and output encodings, eg: mb_convert_encoding() . 如果需要在PHP中转换字符串编码,请使用明确定义输入输出编码的内容,例如: mb_convert_encoding() [you still don't need it for this] [您仍然不需要它]

Second: Whatever you think decbin() does, you're incorrect. 第二:无论您认为decbin()做什么,都是错误的。 It converts an integer into a literal, capital-S String composed of 0 and 1 characters . 它将整数转换为由01 字符组成的文字大写S字符串。

Third: PHP strings have no inherent encoding and are roughly equivalent to byte arrays if you twisted my arm for a description. 第三: PHP字符串没有内在的编码 ,如果您为了描述而绞尽脑汁 ,则大致相当于字节数组。 The bytes you put into them are the bytes you get out of them. 放入其中的字节就是从中取出的字节。

Fourth: I'm not exactly a C# expert [or intermediate, or even beginner ] but that example code is horrendous. 第四:我不是C#专家[或中级,甚至是初学者 ],但示例代码太可怕了。 What even is the significance of the N in connectionId.ToString("N") ? connectionId.ToString("N") N的含义到底是什么? I can't find any documentation about this. 我找不到与此有关的任何文档。

Start simple, use meaningful variable names, build up, and read the docs . 从简单开始,使用有意义的变量名,构建并阅读docs

$connectionID = strtolower('5fecbc200f0e4a7cbf41040e11047e56');
$apiKey       = strtolower('2de51c4fd0f04b9fabeb95225e87da70');
$hash         = hash_hmac("sha1", $connectionID, $apiKey, true);
var_dump(base64_encode($hash));

Output: 输出:

string(28) "m5/Vc1RzhUETQvEtx/JdIglQpTg="

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM