8

this is my PHP code:

hash_hmac( "sha256", utf8_encode( $filename ), utf8_encode( $password ) );

and this is my C# code:

var hmacsha256 = new HMACSHA256( Encoding.UTF8.GetBytes( password ) );
hmacsha256.ComputeHash( Encoding.UTF8.GetBytes( filename ) );

unfortunately both results differ. Can anyone give me a hint?

1 Answer 1

16

My C# is not the best but i got it to work, what you need to do is to convert your byte array results to hex.

PHP

$hash = hash_hmac( "sha256", utf8_encode("Filename"), utf8_encode("Password"));
echo $hash;
// 5fe2ae06ff9828b33fe304545289a3f590bfd948ca9ab731c980379992ef41f1

C#

string password = "Password";
string filename = "Filename";

var hmacsha256 = new HMACSHA256(Encoding.UTF8.GetBytes(password));
hmacsha256.ComputeHash(Encoding.UTF8.GetBytes(filename));

foreach(byte test in hmacsha256.Hash)
{
    Console.Write(test.ToString("X2"));
}
// 5FE2AE06FF9828B33FE304545289A3F590BFD948CA9AB731C980379992EF41F1
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you - I will double check and come back to you this evening. I already used BitConverter.ToString( hmacsha256.Hash ), but I got wrong results.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.