#region 1、获取参数GetSignParamSrc
/// <summary>
/// 逻辑
/// 1、将参数按照首字母进行排序
/// 2、拼接参数=密钥(appSecret)+除密钥外排好序的参数串+密钥(appSecret)
/// 3、将参数转换为MD5格式并转换为大写
///
/// </summary>
private string GetSignParamSrc(DataModel dataModel )
{
string paramSrcStr = string.Empty;
string paramMsd5Str = string.Empty;
if (dataModel !=null)
{
Dictionary<string, string> dics = new Dictionary<string, string>();
dics.Add("vendorCode", dataModel .VendorCode);
dics.Add("putawayTime", dataModel .PutAwayTime.ToString());
paramSrcStr = GetParamSrc(dics);
}
if (!string.IsNullOrEmpty(paramSrcStr))
{
paramMsd5Str= GetMD52(paramSrcStr);
}
return paramMsd5Str;
}
#endregion
#region 2、 参数按照ASCII码从小到大排序
/// <summary>
/// 参数按照ASCII码从小到大排序(字典序)
/// </summary>
/// <param name="paramsMap">参数</param>
/// <returns></returns>
private string GetParamSrc(Dictionary<string, string> paramsMap)
{
var vDic = paramsMap.OrderBy(x => x.Key, new ComparerString()).ToDictionary(x => x.Key, y => y.Value);
var str = new StringBuilder();
foreach (var kv in vDic)
{
var pvalue = kv.Value;
if (string.IsNullOrEmpty(pvalue)) continue;
str.Append(pvalue);
}
var result = str.ToString();
return result;
}
#endregion
#region 3、 MD5签名算法
/// <summary>
/// MD5 32为运算
/// </summary>
/// <param name="myString">需要加密的Str</param>
/// <returns></returns>
public static string GetMD52(string myString)
{
string byte2String = null;
if (!string.IsNullOrWhiteSpace(myString))
{
MD5 md5 = new MD5CryptoServiceProvider();
byte[] fromData = Encoding.UTF8.GetBytes(myString);
byte[] targetData = md5.ComputeHash(fromData);
for (int i = 0; i < targetData.Length; i++)
{
byte2String = byte2String + targetData[i].ToString("x2");
}
byte2String = byte2String.ToUpper(); //转化为大写
}
else
{
LoggerFactory.Error("没有获取到字符串请检查是否有误");
}
return byte2String;
}
#endregion
#region 对比
/// <summary>
/// 对比
/// </summary>
public class ComparerString : IComparer<String>
{
public int Compare(String x, String y)
{
return string.CompareOrdinal(x, y);
}
}
#endregion