首先吐槽一下demo写的确实有点low,从备注到命名纯粹是抄袭java。
其次是两个坑:
private string CalcHMACSHA256Hash(string plaintext, string salt) {
string result = "";
//var enc = Encoding.Default; --中文签名错误
byte[]
baText2BeHashed = Encoding.UTF8.GetBytes(plaintext),
baSalt = Encoding.UTF8.GetBytes(salt);
HMACSHA256 hasher = new HMACSHA256(baSalt);
byte[] baHashedText = hasher.ComputeHash(baText2BeHashed);
result = string.Join("", baHashedText.ToList().Select(b => b.ToString("x2")).ToArray());
return result.ToUpper(); --前端返回总是大写,此处必须大写,否则签名校验失败
}