Core.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Security.Cryptography;
  6. using System.Text;
  7. using System.Web;
  8. namespace EMIS.Utility.OnlinePay.Alipay.Models
  9. {
  10. public class Core
  11. {
  12. public Core()
  13. {
  14. }
  15. /// <summary>
  16. /// 除去数组中的空值和签名参数并以字母a到z的顺序排序
  17. /// </summary>
  18. /// <param name="dicArrayPre">过滤前的参数组</param>
  19. /// <returns>过滤后的参数组</returns>
  20. public static Dictionary<string, string> FilterPara(SortedDictionary<string, string> dicArrayPre)
  21. {
  22. Dictionary<string, string> dicArray = new Dictionary<string, string>();
  23. foreach (KeyValuePair<string, string> temp in dicArrayPre)
  24. {
  25. if (temp.Key.ToLower() != "sign" && temp.Key.ToLower() != "sign_type" && temp.Value != "" && temp.Value != null)
  26. {
  27. dicArray.Add(temp.Key, temp.Value);
  28. }
  29. }
  30. return dicArray;
  31. }
  32. /// <summary>
  33. /// 把数组所有元素,按照“参数=参数值”的模式用“&”字符拼接成字符串
  34. /// </summary>
  35. /// <param name="sArray">需要拼接的数组</param>
  36. /// <returns>拼接完成以后的字符串</returns>
  37. public static string CreateLinkString(Dictionary<string, string> dicArray)
  38. {
  39. StringBuilder prestr = new StringBuilder();
  40. foreach (KeyValuePair<string, string> temp in dicArray)
  41. {
  42. prestr.Append(temp.Key + "=" + temp.Value + "&");
  43. }
  44. //去掉最後一個&字符
  45. int nLen = prestr.Length;
  46. prestr.Remove(nLen - 1, 1);
  47. return prestr.ToString();
  48. }
  49. /// <summary>
  50. /// 把数组所有元素,按照“参数=参数值”的模式用“&”字符拼接成字符串,并对参数值做urlencode
  51. /// </summary>
  52. /// <param name="sArray">需要拼接的数组</param>
  53. /// <param name="code">字符编码</param>
  54. /// <returns>拼接完成以后的字符串</returns>
  55. //public static string CreateLinkStringUrlencode(Dictionary<string, string> dicArray, Encoding code)
  56. //{
  57. // StringBuilder prestr = new StringBuilder();
  58. // foreach (KeyValuePair<string, string> temp in dicArray)
  59. // {
  60. // prestr.Append(temp.Key + "=" + HttpUtility.UrlEncode(temp.Value, code) + "&");
  61. // }
  62. // //去掉最後一個&字符
  63. // int nLen = prestr.Length;
  64. // prestr.Remove(nLen - 1, 1);
  65. // return prestr.ToString();
  66. //}
  67. /// <summary>
  68. /// 获取文件的md5摘要
  69. /// </summary>
  70. /// <param name="sFile">文件流</param>
  71. /// <returns>MD5摘要结果</returns>
  72. public static string GetAbstractToMD5(Stream sFile)
  73. {
  74. MD5 md5 = new MD5CryptoServiceProvider();
  75. byte[] result = md5.ComputeHash(sFile);
  76. StringBuilder sb = new StringBuilder(32);
  77. for (int i = 0; i < result.Length; i++)
  78. {
  79. sb.Append(result[i].ToString("x").PadLeft(2, '0'));
  80. }
  81. return sb.ToString();
  82. }
  83. /// <summary>
  84. /// 获取文件的md5摘要
  85. /// </summary>
  86. /// <param name="dataFile">文件流</param>
  87. /// <returns>MD5摘要结果</returns>
  88. public static string GetAbstractToMD5(byte[] dataFile)
  89. {
  90. MD5 md5 = new MD5CryptoServiceProvider();
  91. byte[] result = md5.ComputeHash(dataFile);
  92. StringBuilder sb = new StringBuilder(32);
  93. for (int i = 0; i < result.Length; i++)
  94. {
  95. sb.Append(result[i].ToString("x").PadLeft(2, '0'));
  96. }
  97. return sb.ToString();
  98. }
  99. }
  100. }