StringHelper.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Reflection;
  7. using System.Security.Cryptography;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. using ConsoleHttp.Model;
  11. namespace MqttMsgServer.Tools
  12. {
  13. public static class StringHelper
  14. {
  15. /// <summary>
  16. /// MD5字符串加密
  17. /// </summary>
  18. /// <param name="txt"></param>
  19. /// <returns>加密后字符串</returns>
  20. public static string GenerateMD5(string txt)
  21. {
  22. using (MD5 mi = MD5.Create())
  23. {
  24. byte[] buffer = Encoding.Default.GetBytes(txt);
  25. //开始加密
  26. byte[] newBuffer = mi.ComputeHash(buffer);
  27. StringBuilder sb = new StringBuilder();
  28. for (int i = 0; i < newBuffer.Length; i++)
  29. {
  30. sb.Append(newBuffer[i].ToString("x2"));
  31. }
  32. return sb.ToString();
  33. }
  34. }
  35. /// <summary>
  36. /// MD5流加密
  37. /// </summary>
  38. /// <param name="inputStream"></param>
  39. /// <returns></returns>
  40. public static string GenerateMD5(Stream inputStream)
  41. {
  42. using (MD5 mi = MD5.Create())
  43. {
  44. //开始加密
  45. byte[] newBuffer = mi.ComputeHash(inputStream);
  46. StringBuilder sb = new StringBuilder();
  47. for (int i = 0; i < newBuffer.Length; i++)
  48. {
  49. sb.Append(newBuffer[i].ToString("x2"));
  50. }
  51. return sb.ToString();
  52. }
  53. }
  54. public static ResponseResult CheckError(string errorMsg, int returnCode = 9001)
  55. {
  56. ResponseResult result = new ResponseResult {IsSuccess = false, ErrorMessage = errorMsg, ReturnCode = 9001};
  57. return result;
  58. }
  59. /// <summary>
  60. /// Base64 编码
  61. /// </summary>
  62. /// <param name="encode">编码方式</param>
  63. /// <param name="source">要编码的字符串</param>
  64. /// <returns>返回编码后的字符串</returns>
  65. public static string EncodeBase64(this string source, Encoding encode = null)
  66. {
  67. string result;
  68. try
  69. {
  70. encode = encode ?? Encoding.UTF8;
  71. byte[] bytes = encode.GetBytes(source);
  72. result = Convert.ToBase64String(bytes);
  73. }
  74. catch
  75. {
  76. result = source;
  77. }
  78. return result;
  79. }
  80. /// <summary>
  81. /// Base64 解码
  82. /// </summary>
  83. /// <param name="encode">解码方式</param>
  84. /// <param name="source">要解码的字符串</param>
  85. /// <returns>返回解码后的字符串</returns>
  86. public static string DecodeBase64(this string source, Encoding encode = null)
  87. {
  88. string result;
  89. try
  90. {
  91. byte[] bytes = Convert.FromBase64String(source);
  92. encode = encode ?? Encoding.UTF8;
  93. result = encode.GetString(bytes);
  94. }
  95. catch
  96. {
  97. result = source;
  98. }
  99. return result;
  100. }
  101. public static int ToInt(this string s, int defaultValue = 0)
  102. {
  103. int i;
  104. return int.TryParse(s, out i) ? i : defaultValue;
  105. }
  106. public static List<T> ToList<T>(DataTable dt) where T : class, new()
  107. {
  108. Type t = typeof(T);
  109. PropertyInfo[] propertyInfo = t.GetProperties();
  110. List<T> list = new List<T>();
  111. foreach (DataRow item in dt.Rows)
  112. {
  113. T obj = new T();
  114. foreach (PropertyInfo s in propertyInfo)
  115. {
  116. var typeName = s.Name;
  117. if (dt.Columns.Contains(typeName))
  118. {
  119. if (!s.CanWrite) continue;
  120. object value = item[typeName];
  121. if (value == DBNull.Value) continue;
  122. s.SetValue(obj, s.PropertyType == typeof(string) ? value.ToString() : value, null);
  123. }
  124. }
  125. list.Add(obj);
  126. }
  127. return list;
  128. }
  129. }
  130. public static class ExtendEnum
  131. {
  132. public static int ToInt(this System.Enum e)
  133. {
  134. return e.GetHashCode();
  135. }
  136. }
  137. }