using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Web;
namespace EMIS.Utility.OnlinePay.Alipay.Models
{
public class Core
{
public Core()
{
}
///
/// 除去数组中的空值和签名参数并以字母a到z的顺序排序
///
/// 过滤前的参数组
/// 过滤后的参数组
public static Dictionary FilterPara(SortedDictionary dicArrayPre)
{
Dictionary dicArray = new Dictionary();
foreach (KeyValuePair temp in dicArrayPre)
{
if (temp.Key.ToLower() != "sign" && temp.Key.ToLower() != "sign_type" && temp.Value != "" && temp.Value != null)
{
dicArray.Add(temp.Key, temp.Value);
}
}
return dicArray;
}
///
/// 把数组所有元素,按照“参数=参数值”的模式用“&”字符拼接成字符串
///
/// 需要拼接的数组
/// 拼接完成以后的字符串
public static string CreateLinkString(Dictionary dicArray)
{
StringBuilder prestr = new StringBuilder();
foreach (KeyValuePair temp in dicArray)
{
prestr.Append(temp.Key + "=" + temp.Value + "&");
}
//去掉最後一個&字符
int nLen = prestr.Length;
prestr.Remove(nLen - 1, 1);
return prestr.ToString();
}
///
/// 把数组所有元素,按照“参数=参数值”的模式用“&”字符拼接成字符串,并对参数值做urlencode
///
/// 需要拼接的数组
/// 字符编码
/// 拼接完成以后的字符串
//public static string CreateLinkStringUrlencode(Dictionary dicArray, Encoding code)
//{
// StringBuilder prestr = new StringBuilder();
// foreach (KeyValuePair temp in dicArray)
// {
// prestr.Append(temp.Key + "=" + HttpUtility.UrlEncode(temp.Value, code) + "&");
// }
// //去掉最後一個&字符
// int nLen = prestr.Length;
// prestr.Remove(nLen - 1, 1);
// return prestr.ToString();
//}
///
/// 获取文件的md5摘要
///
/// 文件流
/// MD5摘要结果
public static string GetAbstractToMD5(Stream sFile)
{
MD5 md5 = new MD5CryptoServiceProvider();
byte[] result = md5.ComputeHash(sFile);
StringBuilder sb = new StringBuilder(32);
for (int i = 0; i < result.Length; i++)
{
sb.Append(result[i].ToString("x").PadLeft(2, '0'));
}
return sb.ToString();
}
///
/// 获取文件的md5摘要
///
/// 文件流
/// MD5摘要结果
public static string GetAbstractToMD5(byte[] dataFile)
{
MD5 md5 = new MD5CryptoServiceProvider();
byte[] result = md5.ComputeHash(dataFile);
StringBuilder sb = new StringBuilder(32);
for (int i = 0; i < result.Length; i++)
{
sb.Append(result[i].ToString("x").PadLeft(2, '0'));
}
return sb.ToString();
}
}
}