Reputation: 21
I need c# equivalent of the below nodejs code. I have some c# code, but the results are not the same.
//working node code below
Ice3x.prototype._signMessage = function (message) {
var hmac = crypto.createHmac('sha512',new Buffer(this.secret, 'base64'));
hmac.update(message);
var signature = hmac.digest('base64');
return signature;
}
//c# code below
public class HmacSignatureCalculator : ICalculteSignature
{
public string Signature(string secret, string value)
{
var secretBytes = Encoding.UTF8.GetBytes(secret);
var valueBytes = Encoding.UTF8.GetBytes(value);
string signature;
using (var hmac = new HMACSHA512(secretBytes))
{
var hash = hmac.ComputeHash(valueBytes);
signature = Convert.ToBase64String(hash);
}
return signature;
}
}
Upvotes: 2
Views: 1431
Reputation: 1038710
It looks like the difference comes from the way the secret is encoded. In the node version it assumes that it represents a base64 encoded byte array, whereas in your C# version you treat it as a normal string.
So in your C# version read the byte array from the base 64 encoded secret:
var secretBytes = Convert.FromBase64String(secret);
Now you are consistent with the node version:
var hmac = crypto.createHmac('sha512', new Buffer(this.secret, 'base64'));
Upvotes: 3