.net6.0 webapi demo

Security.cs 7.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. using System.Data;
  2. using System.Security.Cryptography;
  3. using System.Text;
  4. namespace Net6Demo_Api.Util
  5. {
  6. public static partial class Extention
  7. {
  8. /// <summary>
  9. /// 转换为MD5加密后的字符串(默认加密为32位)
  10. /// </summary>
  11. /// <param name="str"></param>
  12. /// <returns></returns>
  13. public static string ToMD5String(this string str)
  14. {
  15. MD5 md5 = MD5.Create();
  16. byte[] inputBytes = Encoding.UTF8.GetBytes(str);
  17. byte[] hashBytes = md5.ComputeHash(inputBytes);
  18. StringBuilder sb = new StringBuilder();
  19. for (int i = 0; i < hashBytes.Length; i++)
  20. {
  21. sb.Append(hashBytes[i].ToString("x2"));
  22. }
  23. md5.Dispose();
  24. return sb.ToString();
  25. }
  26. /// <summary>
  27. /// 转换为MD5加密后的字符串(16位)
  28. /// </summary>
  29. /// <param name="str"></param>
  30. /// <returns></returns>
  31. public static string ToMD5String16(this string str)
  32. {
  33. return str.ToMD5String().Substring(8, 16);
  34. }
  35. /// <summary>
  36. /// Base64加密
  37. /// 注:默认采用UTF8编码
  38. /// </summary>
  39. /// <param name="source">待加密的明文</param>
  40. /// <returns>加密后的字符串</returns>
  41. public static string Base64Encode(this string source)
  42. {
  43. return Base64Encode(source, Encoding.UTF8);
  44. }
  45. /// <summary>
  46. /// Base64加密
  47. /// </summary>
  48. /// <param name="source">待加密的明文</param>
  49. /// <param name="encoding">加密采用的编码方式</param>
  50. /// <returns></returns>
  51. public static string Base64Encode(this string source, Encoding encoding)
  52. {
  53. string encode = string.Empty;
  54. byte[] bytes = encoding.GetBytes(source);
  55. try
  56. {
  57. encode = Convert.ToBase64String(bytes);
  58. }
  59. catch
  60. {
  61. encode = source;
  62. }
  63. return encode;
  64. }
  65. /// <summary>
  66. /// Base64解密
  67. /// 注:默认使用UTF8编码
  68. /// </summary>
  69. /// <param name="result">待解密的密文</param>
  70. /// <returns>解密后的字符串</returns>
  71. public static string Base64Decode(this string result)
  72. {
  73. return Base64Decode(result, Encoding.UTF8);
  74. }
  75. /// <summary>
  76. /// Base64解密
  77. /// </summary>
  78. /// <param name="result">待解密的密文</param>
  79. /// <param name="encoding">解密采用的编码方式,注意和加密时采用的方式一致</param>
  80. /// <returns>解密后的字符串</returns>
  81. public static string Base64Decode(this string result, Encoding encoding)
  82. {
  83. string decode = string.Empty;
  84. byte[] bytes = Convert.FromBase64String(result);
  85. try
  86. {
  87. decode = encoding.GetString(bytes);
  88. }
  89. catch
  90. {
  91. decode = result;
  92. }
  93. return decode;
  94. }
  95. /// <summary>
  96. /// Base64Url编码
  97. /// </summary>
  98. /// <param name="text">待编码的文本字符串</param>
  99. /// <returns>编码的文本字符串</returns>
  100. public static string Base64UrlEncode(this string text)
  101. {
  102. var plainTextBytes = Encoding.UTF8.GetBytes(text);
  103. var base64 = Convert.ToBase64String(plainTextBytes).Replace('+', '-').Replace('/', '_').TrimEnd('=');
  104. return base64;
  105. }
  106. /// <summary>
  107. /// Base64Url解码
  108. /// </summary>
  109. /// <param name="base64UrlStr">使用Base64Url编码后的字符串</param>
  110. /// <returns>解码后的内容</returns>
  111. public static string Base64UrlDecode(this string base64UrlStr)
  112. {
  113. base64UrlStr = base64UrlStr.Replace('-', '+').Replace('_', '/');
  114. switch (base64UrlStr.Length % 4)
  115. {
  116. case 2:
  117. base64UrlStr += "==";
  118. break;
  119. case 3:
  120. base64UrlStr += "=";
  121. break;
  122. }
  123. var bytes = Convert.FromBase64String(base64UrlStr);
  124. return Encoding.UTF8.GetString(bytes);
  125. }
  126. /// <summary>
  127. /// 计算SHA1摘要
  128. /// 注:默认使用UTF8编码
  129. /// </summary>
  130. /// <param name="str">字符串</param>
  131. /// <returns></returns>
  132. public static byte[] ToSHA1Bytes(this string str)
  133. {
  134. return str.ToSHA1Bytes(Encoding.UTF8);
  135. }
  136. /// <summary>
  137. /// 计算SHA1摘要
  138. /// </summary>
  139. /// <param name="str">字符串</param>
  140. /// <param name="encoding">编码</param>
  141. /// <returns></returns>
  142. public static byte[] ToSHA1Bytes(this string str, Encoding encoding)
  143. {
  144. SHA1 sha1 = SHA1.Create();
  145. byte[] inputBytes = encoding.GetBytes(str);
  146. byte[] outputBytes = sha1.ComputeHash(inputBytes);
  147. return outputBytes;
  148. }
  149. /// <summary>
  150. /// 转为SHA1哈希加密字符串
  151. /// 注:默认使用UTF8编码
  152. /// </summary>
  153. /// <param name="str">字符串</param>
  154. /// <returns></returns>
  155. public static string ToSHA1String(this string str)
  156. {
  157. return str.ToSHA1String(Encoding.UTF8);
  158. }
  159. /// <summary>
  160. /// 转为SHA1哈希
  161. /// </summary>
  162. /// <param name="str">字符串</param>
  163. /// <param name="encoding">编码</param>
  164. /// <returns></returns>
  165. public static string ToSHA1String(this string str, Encoding encoding)
  166. {
  167. byte[] sha1Bytes = str.ToSHA1Bytes(encoding);
  168. string resStr = BitConverter.ToString(sha1Bytes);
  169. return resStr.Replace("-", "").ToLower();
  170. }
  171. /// <summary>
  172. /// SHA256加密
  173. /// </summary>
  174. /// <param name="str">字符串</param>
  175. /// <returns></returns>
  176. public static string ToSHA256String(this string str)
  177. {
  178. byte[] bytes = Encoding.UTF8.GetBytes(str);
  179. byte[] hash = SHA256.Create().ComputeHash(bytes);
  180. StringBuilder builder = new StringBuilder();
  181. for (int i = 0; i < hash.Length; i++)
  182. {
  183. builder.Append(hash[i].ToString("x2"));
  184. }
  185. return builder.ToString();
  186. }
  187. /// <summary>
  188. /// HMACSHA256算法
  189. /// </summary>
  190. /// <param name="text">内容</param>
  191. /// <param name="secret">密钥</param>
  192. /// <returns></returns>
  193. public static string ToHMACSHA256String(this string text, string secret)
  194. {
  195. secret = secret ?? "";
  196. byte[] keyByte = Encoding.UTF8.GetBytes(secret);
  197. byte[] messageBytes = Encoding.UTF8.GetBytes(text);
  198. using (var hmacsha256 = new HMACSHA256(keyByte))
  199. {
  200. byte[] hashmessage = hmacsha256.ComputeHash(messageBytes);
  201. return Convert.ToBase64String(hashmessage).Replace('+', '-').Replace('/', '_').TrimEnd('=');
  202. }
  203. }
  204. }
  205. }