Answer the question
In order to leave comments, you need to log in
Why don't HMAC SHA256 results match for PHP and C#?
Hashes received in PHP and C# do not match. Where is the mistake? Problem in C# algorithm
PHP hash_hmac('sha256',$data, $key);
code: C# code:
//ОСНОВНАЯ ФУНКЦИЯ
public static string HashHMACHex(string keyHex, string message)
{
byte[] hash = HashHMAC(HexDecode(keyHex), StringEncode(message));
return HashEncode(hash);
}
private static byte[] HashHMAC(byte[] key, byte[] message)
{
var hash = new System.Security.Cryptography.HMACSHA256(key);
return hash.ComputeHash(message);
}
private static string HashEncode(byte[] hash)
{
return BitConverter.ToString(hash).Replace("-", "").ToLower();
}
private static byte[] StringEncode(string text)
{
var encoding = new ASCIIEncoding();
return encoding.GetBytes(text);
}
private static byte[] HexDecode(string hex)
{
var bytes = new byte[hex.Length / 2];
for (int i = 0; i < bytes.Length; i++)
{
bytes[i] = byte.Parse(hex.Substring(i * 2, 2), NumberStyles.HexNumber);
}
return bytes;
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question