.NET と C# を使用して、HMAC SHA512 を使用して整合性文字列を PHP サーバーに提供する必要があります。C# での使用:
Encoding encoding = Encoding.UTF8;
byte[] keyByte = encoding.GetBytes(key);
HMACSHA512 hmacsha512 = new HMACSHA512(keyByte);
byte[] messageBytes = encoding.GetBytes(message);
byte[] hashmessage = hmacsha512.ComputeHash(messageBytes);
return(ByteToString(hashmessage).toUpper());
しかし、PHP の hash_hmac() PHP コードとは一致しません:
$hmac = strtoupper(hash_hmac($pbx_hash, $msg, $binKey));
C# (utf8、ASCII、Unicode) でエンコーディングを変更しようとしましたが、成功しませんでした。
私はネット上で見つけた多くの解決策を試しましたが、同じ文字列を与えるものはありません:(
PHP コードを変更できず、C# の何が問題なのかわかりません
編集これはByteToString
(コメントからコピーされた)です:
static string ByteToString(byte[] buff)
{
string sbinary = "";
for (int i = 0; i < buff.Length; i++)
{
sbinary += buff[i].ToString("X2"); /* hex format */
}
return (sbinary);
}
多くのテストの後、PHP hash_hmac キーがバイト Array ではなく文字列である場合、同じ結果が得られることがわかりました。問題は PHP の変換関数 $binKey = pack("H*", $keyTest); にあるようです。