3 回答

TA貢獻1848條經(jīng)驗 獲得超10個贊
我也在尋找這個,盡管它只是一個小的varibale的錯誤,在hash_hmac函數(shù)中將最后一個值傳遞給true,這將返回原始輸出,當(dāng)您將其轉(zhuǎn)換為base64時,它將給出與c#代碼相同的輸出。
$buffer = $userName.$accessKey.$timeStamp.$originUrl;
$hmac = hash_hmac('sha256', $buffer, $tokenSecret, true);
$authenticationKey = base64_encode($hmac);
只需在 c# 函數(shù)中使用鍵,如下面的代碼所示
var hmac = new System.Security.Cryptography.HMACSHA256();
hmac.Key = System.Text.Encoding.UTF8.GetBytes(tokenSecret);
在我的代碼中,我有bas64形式的tokenSecret變量。

TA貢獻1847條經(jīng)驗 獲得超7個贊
在 c# 代碼中,使用了隨機生成的密鑰。查看文檔:https://docs.microsoft.com/en-us/dotnet/api/system.security.cryptography.hmacsha256?view=netframework-4.8#constructors
HMACSHA256()
使用隨機生成的密鑰初始化 HMACSHA256 類的新實例。
HMACSHA256(字節(jié)[])
使用指定的關(guān)鍵數(shù)據(jù)初始化 HMACSHA256 類的新實例
現(xiàn)在比較php文檔:https://www.php.net/manual/en/function.hash-hmac.php
hash_hmac ( string $algo , string $data , string $key [, bool $raw_output = FALSE ] ) : string
你看,你在php中調(diào)用buffer的東西實際上是鍵,你在c#中調(diào)用buffer的是你散列的數(shù)據(jù)。
因此,應(yīng)包含要散列的數(shù)據(jù)。$string
根據(jù)轉(zhuǎn)換,您不需要在php中獲取字節(jié):這個C#編碼代碼的PHP等效物是什么?
- 3 回答
- 0 關(guān)注
- 124 瀏覽
添加回答
舉報