using .NET and C# i need to provide an integrity string using HMAC SHA512 to a PHP server . Using in 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());
But it doesn't match with PHP hash_hmac() PHP code :
$hmac = strtoupper(hash_hmac($pbx_hash, $msg, $binKey));
I try to change encoding in C# (utf8, ASCII,Unicode) Without success.
I've tried many solution found on the net but nothing give the same string :(
I can't change the PHP code, and doesn't see what's wrong in C#
Edit This is ByteToString
(copied from the comment):
static string ByteToString(byte[] buff) { string sbinary = ""; for (int i = 0; i < buff.Length; i++) { sbinary += buff[i].ToString("X2"); /* hex format */ } return (sbinary); }
After many tets, in found that i get the same results if PHP hash_hmac key is a string, not a byte Array . Seems that the problem is with the PHP convert function $binKey = pack("H*", $keyTest);
C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...
In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.
C is a high-level and general-purpose programming language that is ideal for developing firmware or portable applications. Originally intended for writing system software, C was developed at Bell Labs by Dennis Ritchie for the Unix Operating System in the early 1970s.
C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.
The problem must be the actual representation of the key/message data.
See the following tests:
PHP
#!/usr/bin/php <?php print strtoupper(hash_hmac("sha256", "message", "key")); ?>
Output (live via http://writecodeonline.com/php/):
6E9EF29B75FFFC5B7ABAE527D58FDADB2FE42E7219011976917343065F58ED4A
C#
using System; using System.Text; using System.Security.Cryptography; public class Program { private const string key = "key"; private const string message = "message"; private static readonly Encoding encoding = Encoding.UTF8; static void Main(string[] args) { var keyByte = encoding.GetBytes(key); using (var hmacsha256 = new HMACSHA256(keyByte)) { hmacsha256.ComputeHash(encoding.GetBytes(message)); Console.WriteLine("Result: {0}", ByteToString(hmacsha256.Hash)); } } static string ByteToString(byte[] buff) { string sbinary = ""; for (int i = 0; i < buff.Length; i++) sbinary += buff[i].ToString("X2"); /* hex format */ return sbinary; } }
Output (live via http://ideone.com/JdpeL):
Result: 6E9EF29B75FFFC5B7ABAE527D58FDADB2FE42E7219011976917343065F58ED4A
So, check the character set/encoding of the PHP input data. Also check the actual algorithm (in $pbx_hash
).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With