jokecamp.com
jokecamp.com copied to clipboard
hmac256 issue in c#
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 );
}
}
This is a good point. Thank you. I'll get this updated soon. Feel free to make a pull request if you would like.