我需要使用hmac-sha512算法调用身份验证。要使用密钥加密的邮件(例如: vU372pd8pS4AN2XEzt2aQn6CRq6dT5 )是:
“ entityReference = 3000001222&caseReference = SilviaTemplate1&type = 16&name = example_WS2.jpg&content = [binary]” 。
我的代码:
public string ChecksumCalc(string entityReference, string caseReference, string type,string name, byte[] content, string key)
{
string message = "entityReference=" + entityReference + "&caseReference=" + caseReference + "&type=" + type + "&name=" + name + "&content=" + BitConverter.ToString(content).Replace("-",string.Empty).ToLower();
string checksum = "";
try
{
var encoding = new System.Text.ASCIIEncoding();
byte[] keyByte = encoding.GetBytes(key);
byte[] messageBytes = encoding.GetBytes(message);
using (var hmacsha512 = new HMACSHA512(keyByte))
{
byte[] hashmessage = hmacsha512.ComputeHash(messageBytes);
//First method
checksum = BitConverter.ToString(hashmessage).Replace("+", "").Replace("/", "").Replace("=", "").Replace("-", "").ToLower();
//Second method
StringBuilder hex = new StringBuilder(hashmessage.Length * 2);
foreach (byte b in hashmessage)
hex.AppendFormat("{0:x2}", b);
checksum = hex.ToString();
//Same result
}
Console.WriteLine(checksum);
}
catch (Exception ex)
{
log.Error("Error on checksumCalc. Exception: " + ex.Message);
throw ex;
}
return checksum;
}
我评论了我的其他尝试可以解决我的问题,但是没有用。