人民医院API

String.cs 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Security.Cryptography;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. namespace RMYY_CallCenter_Api.Utility
  8. {
  9. public static partial class StringExtention
  10. {
  11. /// <summary>
  12. /// 转为bool
  13. /// </summary>
  14. /// <param name="str">字符串</param>
  15. /// <returns></returns>
  16. public static bool ToBool(this string str)
  17. {
  18. return bool.Parse(str);
  19. }
  20. /// <summary>
  21. /// 转为字节数组
  22. /// </summary>
  23. /// <param name="base64Str">base64字符串</param>
  24. /// <returns></returns>
  25. public static byte[] ToBytes_FromBase64Str(this string base64Str)
  26. {
  27. return Convert.FromBase64String(base64Str);
  28. }
  29. /// <summary>
  30. /// 转换为MD5加密后的字符串(默认加密为32位)
  31. /// </summary>
  32. /// <param name="str"></param>
  33. /// <returns></returns>
  34. public static string ToMD5String(this string str)
  35. {
  36. MD5 md5 = MD5.Create();
  37. byte[] inputBytes = Encoding.UTF8.GetBytes(str);
  38. byte[] hashBytes = md5.ComputeHash(inputBytes);
  39. StringBuilder sb = new StringBuilder();
  40. for (int i = 0; i < hashBytes.Length; i++)
  41. {
  42. sb.Append(hashBytes[i].ToString("x2"));
  43. }
  44. md5.Dispose();
  45. return sb.ToString();
  46. }
  47. /// <summary>
  48. /// 转换为MD5加密后的字符串(16位)
  49. /// </summary>
  50. /// <param name="str"></param>
  51. /// <returns></returns>
  52. public static string ToMD5String16(this string str)
  53. {
  54. return str.ToMD5String().Substring(8, 16);
  55. }
  56. /// <summary>
  57. /// Base64加密
  58. /// 注:默认采用UTF8编码
  59. /// </summary>
  60. /// <param name="source">待加密的明文</param>
  61. /// <returns>加密后的字符串</returns>
  62. public static string Base64Encode(this string source)
  63. {
  64. return Base64Encode(source, Encoding.UTF8);
  65. }
  66. /// <summary>
  67. /// Base64加密
  68. /// </summary>
  69. /// <param name="source">待加密的明文</param>
  70. /// <param name="encoding">加密采用的编码方式</param>
  71. /// <returns></returns>
  72. public static string Base64Encode(this string source, Encoding encoding)
  73. {
  74. string encode = string.Empty;
  75. byte[] bytes = encoding.GetBytes(source);
  76. try
  77. {
  78. encode = Convert.ToBase64String(bytes);
  79. }
  80. catch
  81. {
  82. encode = source;
  83. }
  84. return encode;
  85. }
  86. /// <summary>
  87. /// Base64解密
  88. /// 注:默认使用UTF8编码
  89. /// </summary>
  90. /// <param name="result">待解密的密文</param>
  91. /// <returns>解密后的字符串</returns>
  92. public static string Base64Decode(this string result)
  93. {
  94. return Base64Decode(result, Encoding.UTF8);
  95. }
  96. /// <summary>
  97. /// Base64解密
  98. /// </summary>
  99. /// <param name="result">待解密的密文</param>
  100. /// <param name="encoding">解密采用的编码方式,注意和加密时采用的方式一致</param>
  101. /// <returns>解密后的字符串</returns>
  102. public static string Base64Decode(this string result, Encoding encoding)
  103. {
  104. string decode = string.Empty;
  105. byte[] bytes = Convert.FromBase64String(result);
  106. try
  107. {
  108. decode = encoding.GetString(bytes);
  109. }
  110. catch
  111. {
  112. decode = result;
  113. }
  114. return decode;
  115. }
  116. /// <summary>
  117. /// Base64Url编码
  118. /// </summary>
  119. /// <param name="text">待编码的文本字符串</param>
  120. /// <returns>编码的文本字符串</returns>
  121. public static string Base64UrlEncode(this string text)
  122. {
  123. var plainTextBytes = Encoding.UTF8.GetBytes(text);
  124. var base64 = Convert.ToBase64String(plainTextBytes).Replace('+', '-').Replace('/', '_').TrimEnd('=');
  125. return base64;
  126. }
  127. /// <summary>
  128. /// Base64Url解码
  129. /// </summary>
  130. /// <param name="base64UrlStr">使用Base64Url编码后的字符串</param>
  131. /// <returns>解码后的内容</returns>
  132. public static string Base64UrlDecode(this string base64UrlStr)
  133. {
  134. base64UrlStr = base64UrlStr.Replace('-', '+').Replace('_', '/');
  135. switch (base64UrlStr.Length % 4)
  136. {
  137. case 2:
  138. base64UrlStr += "==";
  139. break;
  140. case 3:
  141. base64UrlStr += "=";
  142. break;
  143. }
  144. var bytes = Convert.FromBase64String(base64UrlStr);
  145. return Encoding.UTF8.GetString(bytes);
  146. }
  147. /// <summary>
  148. /// 计算SHA1摘要
  149. /// 注:默认使用UTF8编码
  150. /// </summary>
  151. /// <param name="str">字符串</param>
  152. /// <returns></returns>
  153. public static byte[] ToSHA1Bytes(this string str)
  154. {
  155. return str.ToSHA1Bytes(Encoding.UTF8);
  156. }
  157. /// <summary>
  158. /// 计算SHA1摘要
  159. /// </summary>
  160. /// <param name="str">字符串</param>
  161. /// <param name="encoding">编码</param>
  162. /// <returns></returns>
  163. public static byte[] ToSHA1Bytes(this string str, Encoding encoding)
  164. {
  165. SHA1 sha1 = new SHA1CryptoServiceProvider();
  166. byte[] inputBytes = encoding.GetBytes(str);
  167. byte[] outputBytes = sha1.ComputeHash(inputBytes);
  168. return outputBytes;
  169. }
  170. /// <summary>
  171. /// 转为SHA1哈希加密字符串
  172. /// 注:默认使用UTF8编码
  173. /// </summary>
  174. /// <param name="str">字符串</param>
  175. /// <returns></returns>
  176. public static string ToSHA1String(this string str)
  177. {
  178. return str.ToSHA1String(Encoding.UTF8);
  179. }
  180. /// <summary>
  181. /// 转为SHA1哈希
  182. /// </summary>
  183. /// <param name="str">字符串</param>
  184. /// <param name="encoding">编码</param>
  185. /// <returns></returns>
  186. public static string ToSHA1String(this string str, Encoding encoding)
  187. {
  188. byte[] sha1Bytes = str.ToSHA1Bytes(encoding);
  189. string resStr = BitConverter.ToString(sha1Bytes);
  190. return resStr.Replace("-", "").ToLower();
  191. }
  192. /// <summary>
  193. /// SHA256加密
  194. /// </summary>
  195. /// <param name="str">字符串</param>
  196. /// <returns></returns>
  197. public static string ToSHA256String(this string str)
  198. {
  199. byte[] bytes = Encoding.UTF8.GetBytes(str);
  200. byte[] hash = SHA256.Create().ComputeHash(bytes);
  201. StringBuilder builder = new StringBuilder();
  202. for (int i = 0; i < hash.Length; i++)
  203. {
  204. builder.Append(hash[i].ToString("x2"));
  205. }
  206. return builder.ToString();
  207. }
  208. /// <summary>
  209. /// HMACSHA256算法
  210. /// </summary>
  211. /// <param name="text">内容</param>
  212. /// <param name="secret">密钥</param>
  213. /// <returns></returns>
  214. public static string ToHMACSHA256String(this string text, string secret)
  215. {
  216. secret = secret ?? "";
  217. byte[] keyByte = Encoding.UTF8.GetBytes(secret);
  218. byte[] messageBytes = Encoding.UTF8.GetBytes(text);
  219. using (var hmacsha256 = new HMACSHA256(keyByte))
  220. {
  221. byte[] hashmessage = hmacsha256.ComputeHash(messageBytes);
  222. return Convert.ToBase64String(hashmessage).Replace('+', '-').Replace('/', '_').TrimEnd('=');
  223. }
  224. }
  225. /// <summary>
  226. /// string转int
  227. /// </summary>
  228. /// <param name="str">字符串</param>
  229. /// <returns></returns>
  230. public static int ToInt(this string str)
  231. {
  232. str = str.Replace("\0", "");
  233. if (string.IsNullOrEmpty(str))
  234. return 0;
  235. return Convert.ToInt32(str);
  236. }
  237. /// <summary>
  238. /// string转long
  239. /// </summary>
  240. /// <param name="str">字符串</param>
  241. /// <returns></returns>
  242. public static long ToLong(this string str)
  243. {
  244. str = str?.Replace("\0", "");
  245. if (string.IsNullOrEmpty(str))
  246. return 0;
  247. return Convert.ToInt64(str);
  248. }
  249. /// <summary>
  250. /// 转换为double
  251. /// </summary>
  252. /// <param name="str">字符串</param>
  253. /// <returns></returns>
  254. public static double ToDouble(this string str)
  255. {
  256. return Convert.ToDouble(str);
  257. }
  258. /// <summary>
  259. /// string转byte[]
  260. /// </summary>
  261. /// <param name="str">字符串</param>
  262. /// <returns></returns>
  263. public static byte[] ToBytes(this string str)
  264. {
  265. return Encoding.Default.GetBytes(str);
  266. }
  267. /// <summary>
  268. /// string转byte[]
  269. /// </summary>
  270. /// <param name="str">字符串</param>
  271. /// <param name="theEncoding">需要的编码</param>
  272. /// <returns></returns>
  273. public static byte[] ToBytes(this string str, Encoding theEncoding)
  274. {
  275. return theEncoding.GetBytes(str);
  276. }
  277. /// <summary>
  278. /// 转换为日期格式
  279. /// </summary>
  280. /// <param name="str"></param>
  281. /// <returns></returns>
  282. public static DateTime ToDateTime(this string str)
  283. {
  284. return Convert.ToDateTime(str);
  285. }
  286. /// <summary>
  287. /// 转为首字母大写
  288. /// </summary>
  289. /// <param name="str">字符串</param>
  290. /// <returns></returns>
  291. public static string ToFirstUpperStr(this string str)
  292. {
  293. return str.Substring(0, 1).ToUpper() + str.Substring(1);
  294. }
  295. /// <summary>
  296. /// 转为首字母小写
  297. /// </summary>
  298. /// <param name="str">字符串</param>
  299. /// <returns></returns>
  300. public static string ToFirstLowerStr(this string str)
  301. {
  302. return str.Substring(0, 1).ToLower() + str.Substring(1);
  303. }
  304. /// <summary>
  305. /// 将枚举类型的文本转为枚举类型
  306. /// </summary>
  307. /// <typeparam name="TEnum">枚举类型</typeparam>
  308. /// <param name="enumText">枚举文本</param>
  309. /// <returns></returns>
  310. public static TEnum ToEnum<TEnum>(this string enumText) where TEnum : struct
  311. {
  312. Enum.TryParse(enumText, out TEnum value);
  313. return value;
  314. }
  315. }
  316. }