jokecamp / jokecamp.com

personal blog and website
https://www.jokecamp.com
84 stars 40 forks source link

hmac256 issue in c# #26

Open ClayBorkholm opened 5 years ago

ClayBorkholm commented 5 years ago

Thanks for compiling the list - it's very helpful. The c# code works, but it won't work more broadly in that you've used an encoding that is different than most of the other examples. In particular, you're using System.Text.ASCIIEncoding() and you should be using System.Text.UTF8Encoding(). The results are identical with the simple test, but will fail with extended Unicode characters.

string CreateToken( string message, string secret )
{
   var encoding = new System.Text.UTF8Encoding( );
   var keyBytes = encoding.GetBytes( secret );
   var messageBytes = encoding.GetBytes( Message );
   using ( var hmacsha256 = new HMACSHA256( keyBytes ) )
   {
     var hashmessage = hmacsha256.ComputeHash( messageBytes );
     return Convert.ToBase64String( hashmessage );
   }
}
jokecamp commented 5 years ago

This is a good point. Thank you. I'll get this updated soon. Feel free to make a pull request if you would like.