[英]How can I replicate this C# hashing in PHP? (toByteArray(), ComputeHash())
我正在嘗試在PHP
復制以下代碼,它是我必須與之交互的 API 的示例代碼(API 和示例代碼在C#
,我的應用在PHP 5.3
)。 我不是 C# 開發人員,因此在執行此操作時遇到了麻煩。
// C# Code I am trying to replicate in PHP
var apiTokenId = 1887;
var apiToken = "E1024763-1234-5678-91E0-T32E4E7EB316";
// Used to authenticate our request by the API (which is in C#)
var stringToSign = string.Empty;
stringToSign += "POST"+"UserAgent"+"http://api.com/post";
// Here is the issue, How can I do the following 3 lines in PHP?
// No "secret key" provided?.. How do I do this in PHP?
var hmacsha1 = new HMACSHA1(new Guid(apiToken).toByteArray());
// Make a byte array with ASCII encoding.
byte[] byteArray = System.Text.Encoding.ASCII.GetBytes(stringToSign);
// Finally, 'computeHash' of the above (what does this do exactly?!)
var calculatedSignature = Convert.ToBase64String(hmacsha1.ComputeHash(byteArray));
我已經使用pack()
和我在網上找到的其他函數嘗試了許多變體,但沒有任何可比較的東西,我不知道我是否做得對。
任何 C# 開發人員都可以運行上述代碼並發布生成的值,以便我可以使用它來檢查/測試嗎?
我試過檢查MSDN以查看這些方法的作用,但被卡住了(不確定它是否正確,因為我沒有什么可以比較的)。
// Set vars
$apiToken = 'E1024763-1234-5678-91E0-T32E4E7EB316';
$apiTokenId = '1887';
$stringToSign = "POST"."UserAgent"."http://api.com/post";
// HowTo: Build a `byteArray` of our apiToken? (i think)
// C#: var hmacsha1 = new HMACSHA1(new Guid(apiToken).toByteArray());
// HowTo: Convert our $stringToSign to a ASCII encoded `byteArray`?
// C#: byte[] byteArray = System.Text.Encoding.ASCII.GetBytes(stringToSign);
// HowTo: Generate a base64 string of our (`hmacsha1`.ComputeHash(byteArray))
// C#: var calculatedSignature = Convert.ToBase64String(hmacsha1.ComputeHash(byteArray));
這聽起來非常簡單和直接,但我不確定這些 C# 方法中的一些是做什么的..
C#
方法做什么/返回什么?ComputeHash(byteArray)
- 計算到什么?.. 返回什么?System.Text.Encoding.ASCII.GetBytes(stringToSign);
- 這會返回什么?new HMACSHA1(new Guid(apiToken).toByteArray());
No Secret Key?,使用的密鑰是什么?任何資源或幫助將不勝感激。 我在 SO 上嘗試了其他答案的變體,但沒有任何樂趣。
我可以在網上某處運行這 3 行代碼嗎(比如JSFiddle
但對於C#
?),以便我可以看到每一行的輸出?
仍然遇到了這個問題,我已經設法在 Visual Studio 中測試了C#
代碼,但是我無法獲得在PHP
生成的相同哈希值。
.. 將上面的C#
代碼(特別是創建SHA1
哈希的 3 行)轉換為 PHP(查看我上面發布的Pseudo Code
)。 我應該能夠使用 PHP 匹配C#
哈希。
如果您有任何其他問題,請提問。
問題是 GUID 的字符串形式顛倒了 GUID 前 3 個段中 2 個字符的十六進制數字的順序。 有關詳細信息,請參閱示例中的注釋: http : //msdn.microsoft.com/en-us/library/system.guid.tobytearray.aspx
以下代碼應該可以工作:
$apiTokenId = 1887;
$apiToken = "E1024763-1234-5678-91E0-FF2E4E7EB316";
$stringToSign = '';
$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);
}
$stringToSign .= "POST" . "UserAgent" . "http://api.com/post";
$hmacsha1 = base64_encode(hash_hmac('sha1',$stringToSign,$keyStr,true));
我已經針對您在上面提供的 C# 代碼測試了此代碼,並且輸出是相同的。 但是,原始代碼中指定的 GUID 無效,因此我不得不對其稍作更改。
當我不必測試代碼時,這很容易:P
http://php.net/manual/en/function.hash-hmac.php - 這相當於 HMACSHA1 c# 類。
字符串 hash_hmac (字符串 $algo , 字符串 $data , 字符串 $key [, bool $raw_output = false ] )
所以 $algo = "sha1"
$data 是你的 $stringToSign - 因為它已經是一個 ascii 字符串(我希望) - C# 只是采用了相同的字節。
new Guid(apiToken).toByteArray() -> 這是 GUID 的 16 字節 (16*8 = 128) 表示 - 即 32*4 = 128 位。 這是關鍵。
$key 是一個字符串,因此您需要與 $apiToken 等效的 ASCII 字符串(這是 32 個十六進制字符 - 第一個去除/忽略中間的破折號) - E10247631234567891E0T32E4E7EB316(更正密鑰 - 它不能有“T”)
function hex2str($hex) {
for($i=0;$i<strlen($hex);$i+=2) $str .= chr(hexdec(substr($hex,$i,2)));
return $str;
}
$hexKey = hex2str($apiToken); //strip the dashes first
http://www.linux-support.com/cms/php-convert-hex-strings-to-ascii-strings/
所以方法調用現在有效:
$almostResult = hash_hmac ("sha1" , $stringToSign, $hexKey, true)
這將返回一個二進制字符串 - 您需要將其轉換為 base64 編碼。
$final = base64_encode ($almostResult)
那應該這樣做......享受:)
我遇到了幾乎同樣的問題,經過一些谷歌搜索后,我發現了這篇文章: https : //www.reddit.com/r/PHP/comments/2k9tol/string_to_byte_array_using_utf8_encoding/
在 PHP 中字符串已經是字節數組。 您遇到的具體問題是什么?
對我來說,解決方案只是base64_encode('apikey')
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.