ASP.NET C# 实现钉钉签名算法
在 https://open-doc.dingtalk.com/microapp/faquestions/hxs5v9 钉钉给出了JAVA/PHP算法,下面是C#算法
using System.Security.Cryptography using System.Text; private string hash_hmac2(string message, string secret) { secret = secret ?? ""; var encoding = new System.Text.ASCIIEncoding(); byte[] keyByte = encoding.GetBytes(secret); byte[] messageBytes = encoding.GetBytes(message); using (var hmacsha256 = new HMACSHA256(keyByte)) { byte[] hashmessage = hmacsha256.ComputeHash(messageBytes); return Convert.ToBase64String(hashmessage); } }
下面是调用代码:
login timespan=1546084445901; string appsec = "testappSecret"; sign= hash_hmac2(timespan.ToString(),appsec);
//此时sign为 HCbG3xNE3vzhO+u7qCUL1jS5hsu2n5r2cFhnTrtyDAE=
Response.Write(sign);
下面是获取时间戳
public long GetTimeStamp() { TimeSpan ts = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0); return Convert.ToInt64(ts.TotalSeconds); }