MD5签名
/// <summary>
/// 校验签名
/// </summary>
/// <param name="model">实体</param>
/// <param name="sign">签名</param>
/// <returns></returns>
public static bool CheckSign(object model, string sign)
{
SortedDictionary<string, string> signParams = new SortedDictionary<string, string>(StringComparer.Ordinal);
var keyValueTo = ToMap(model);
foreach (var p in keyValueTo)
{
if (p.Key != "Sign" && p.Key != "Signlen" && p.Key != "Img")
{
signParams[p.Key] = p.Value;
}
}
StringBuilder paramStr = new StringBuilder();
foreach (var item in signParams)
{
if (string.IsNullOrWhiteSpace(item.Value) == false)
{
paramStr.AppendFormat("{0}={1}&", item.Key, item.Value);
}
}
paramStr.AppendFormat("Key={0}", api_secret);
string computeSign = GetMd5Hash(paramStr.ToString()).ToUpper();
return string.Equals(sign, computeSign);
}
/// <summary>
/// 生成签名
/// </summary>
/// <param name="model">实体</param>
/// <returns></returns>
public static string GetSign(object model)
{
SortedDictionary<string, string> signParams = new SortedDictionary<string, string>(StringComparer.Ordinal);
var keyValueTo = ToMap(model);
foreach (var p in keyValueTo)
{
if (p.Key != "Sign" && p.Key != "Signlen" && p.Key != "Img")
{
signParams[p.Key] = p.Value;
}
}
StringBuilder paramStr = new StringBuilder();
foreach (var item in signParams)
{
if (string.IsNullOrWhiteSpace(item.Value) == false)
{
paramStr.AppendFormat("{0}={1}&", item.Key, item.Value);
}
}
paramStr.AppendFormat("Key={0}", api_secret);
string computeSign = GetMd5Hash(paramStr.ToString()).ToUpper();
return computeSign;
}
/// <summary>
/// 将对象属性转换为key-value对
/// </summary>
/// <param name="o"></param>
/// <returns></returns>
private static Dictionary<String, String> ToMap(Object o)
{
Dictionary<String, String> map = new Dictionary<String, String>();
Type t = o.GetType();
PropertyInfo[] pi = t.GetProperties(BindingFlags.Public | BindingFlags.Instance);
foreach (PropertyInfo p in pi)
{
MethodInfo mi = p.GetGetMethod();
if (mi != null && mi.IsPublic)
{
map.Add(p.Name, mi.Invoke(o, new String[] { }).ToString());
}
}
return map;
}
/// <summary>
/// MD5加密
/// </summary>
/// <param name="input">字符串</param>
/// <returns></returns>
public static string GetMd5Hash(string input)
{
// Create a new instance of the MD5CryptoServiceProvider object.
MD5 md5Hasher = MD5.Create();
// Convert the input string to a byte array and compute the hash.
byte[] data = md5Hasher.ComputeHash(Encoding.UTF8.GetBytes(input));
// Create a new Stringbuilder to collect the bytes
// and create a string.
StringBuilder sBuilder = new StringBuilder();
// Loop through each byte of the hashed data
// and format each one as a hexadecimal string.
for (int i = 0; i < data.Length; i++)
{
sBuilder.Append(data[i].ToString("x2"));
}
// Return the hexadecimal string.
return sBuilder.ToString();
}
public static string api_secret { get; set; }
上一篇: T4模板简单了解
推荐阅读
-
PHP中MD5函数使用实例代码
-
Linux系统递归生成目录中文件的md5的方法_PHP
-
利用PHP脚本在Linux下用md5函数加密字符串的方法,linuxmd5
-
用PHP写的MD5加密函数_PHP
-
1亿个32位的md5的密码值,怎样查询其中一个md5值是否存在效率最快?
-
php接口数据加密、解密、验证签名
-
php 遍历目录,生成目录下每个文件的md5值并写入到结果文件中
-
Photoshop制作漂亮的粉红色手绘古典人物签名效果图
-
node.JS的crypto加密模块使用方法详解(MD5,AES,Hmac,Diffie-Hellman加密)
-
PHP、Java、C#实现URI参数签名算法,确保应用与REST服务器之间的