62 lines
1.9 KiB
C#
62 lines
1.9 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Security.Cryptography;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace DG.Tool
|
|
{
|
|
public static class SignHelper
|
|
{
|
|
/// <summary>
|
|
/// 计算签名
|
|
/// </summary>
|
|
/// <param name="param"></param>
|
|
/// <param name="timestamps"></param>
|
|
/// <returns></returns>
|
|
public static string GetSign(string appId, string appSecret, Dictionary<string, object> param, string timestamps)
|
|
{
|
|
|
|
//一次排序
|
|
var newP = param.OrderBy(m => m.Key).ToDictionary(m => m.Key, n => n.Value);
|
|
var pJosn = JsonHelper.ToJson(newP);
|
|
|
|
//二次排序
|
|
var enStrList = new string[] { appId, pJosn, appSecret, timestamps };
|
|
Array.Sort(enStrList, string.CompareOrdinal);
|
|
|
|
//拼接
|
|
var enStr = string.Join("", enStrList);
|
|
//md5 加密
|
|
return GetMd5Hash(enStr);
|
|
}
|
|
/// <summary>
|
|
/// 计算 md5
|
|
/// </summary>
|
|
/// <param name="enCode"></param>
|
|
/// <returns></returns>
|
|
private static string GetMd5Hash(string enCode)
|
|
{
|
|
string res = "";
|
|
byte[] data = Encoding.GetEncoding("utf-8").GetBytes(enCode);
|
|
MD5 md5 = MD5.Create();
|
|
byte[] bytes = md5.ComputeHash(data);
|
|
for (int i = 0; i < bytes.Length; i++)
|
|
{
|
|
res += bytes[i].ToString("x2");
|
|
}
|
|
return res;
|
|
}
|
|
/// <summary>
|
|
/// 获取时间戳
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public static string GetTimeStamp()
|
|
{
|
|
TimeSpan ts = DateTime.Now - new DateTime(1970, 1, 1, 0, 0, 0, 0);
|
|
return Convert.ToInt64(ts.TotalSeconds).ToString();
|
|
}
|
|
}
|
|
}
|