Utilities.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. using Microsoft.Extensions.Logging;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Threading.Tasks;
  6. using System.Security.Cryptography;
  7. using System.IO;
  8. using System.Text;
  9. using Winsoft.GOV.XF.WebApi.WXCore.Models;
  10. using Senparc.Weixin.MP.AdvancedAPIs.OAuth;
  11. namespace Winsoft.GOV.XF.WebApi.WXCore.Helpers
  12. {
  13. public static class Utilities
  14. {
  15. static ILoggerFactory _loggerFactory;
  16. public static void ConfigureLogger(ILoggerFactory loggerFactory)
  17. {
  18. _loggerFactory = loggerFactory;
  19. }
  20. public static ILogger CreateLogger<T>()
  21. {
  22. if (_loggerFactory == null)
  23. {
  24. throw new InvalidOperationException($"{nameof(ILogger)} is not configured. {nameof(ConfigureLogger)} must be called before use");
  25. //_loggerFactory = new LoggerFactory().AddConsole().AddDebug();
  26. }
  27. return _loggerFactory.CreateLogger<T>();
  28. }
  29. public static string ContentRootPath { get; set; }
  30. public static string AESEncrypt(this string input, string key)
  31. {
  32. byte[] bytesToBeEncrypted = Encoding.UTF8.GetBytes(input);
  33. byte[] passwordBytes = Encoding.UTF8.GetBytes(key);
  34. passwordBytes = SHA256.Create().ComputeHash(passwordBytes);
  35. byte[] bytesEncrypted = AESEncryptBytes(bytesToBeEncrypted, passwordBytes);
  36. string result = Convert.ToBase64String(bytesEncrypted);
  37. return result;
  38. }
  39. private static byte[] AESEncryptBytes(byte[] bytesToBeEncrypted, byte[] passwordBytes)
  40. {
  41. byte[] encryptedBytes = null;
  42. var saltBytes = new byte[9] { 13, 34, 27, 67, 189, 255, 104, 219, 122 };
  43. using (var ms = new MemoryStream())
  44. {
  45. using (var AES = new RijndaelManaged())
  46. {
  47. AES.KeySize = 256;
  48. AES.BlockSize = 128;
  49. var key = new Rfc2898DeriveBytes(passwordBytes, saltBytes, 1000);
  50. AES.Key = key.GetBytes(32);
  51. AES.IV = key.GetBytes(16);
  52. AES.Mode = CipherMode.CBC;
  53. using (var cs = new CryptoStream(ms, AES.CreateEncryptor(),
  54. CryptoStreamMode.Write))
  55. {
  56. cs.Write(bytesToBeEncrypted, 0, bytesToBeEncrypted.Length);
  57. cs.Close();
  58. }
  59. encryptedBytes = ms.ToArray();
  60. }
  61. }
  62. return encryptedBytes;
  63. }
  64. public static string AESDecrypt(this string input, string key)
  65. {
  66. byte[] bytesToBeDecrypted = Convert.FromBase64String(input);
  67. byte[] passwordBytes = Encoding.UTF8.GetBytes(key);
  68. passwordBytes = SHA256.Create().ComputeHash(passwordBytes);
  69. byte[] bytesDecrypted = AESDecryptBytes(bytesToBeDecrypted, passwordBytes);
  70. string result = Encoding.UTF8.GetString(bytesDecrypted);
  71. return result;
  72. }
  73. public static byte[] AESDecryptBytes(byte[] bytesToBeDecrypted, byte[] passwordBytes)
  74. {
  75. byte[] decryptedBytes = null;
  76. var saltBytes = new byte[9] { 13, 34, 27, 67, 189, 255, 104, 219, 122 };
  77. using (var ms = new MemoryStream())
  78. {
  79. using (var AES = new RijndaelManaged())
  80. {
  81. AES.KeySize = 256;
  82. AES.BlockSize = 128;
  83. var key = new Rfc2898DeriveBytes(passwordBytes, saltBytes, 1000);
  84. AES.Key = key.GetBytes(32);
  85. AES.IV = key.GetBytes(16);
  86. AES.Mode = CipherMode.CBC;
  87. using (var cs = new CryptoStream(ms, AES.CreateDecryptor(), CryptoStreamMode.Write))
  88. {
  89. cs.Write(bytesToBeDecrypted, 0, bytesToBeDecrypted.Length);
  90. cs.Close();
  91. }
  92. decryptedBytes = ms.ToArray();
  93. }
  94. }
  95. return decryptedBytes;
  96. }
  97. /// <summary>
  98. /// 日期转换为时间戳(时间戳单位秒)
  99. /// </summary>
  100. /// <param name="TimeStamp"></param>
  101. /// <returns></returns>
  102. public static long ToTimeStamp(this DateTime time)
  103. {
  104. DateTime Jan1st1970 = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
  105. return (long) (time.AddHours(-8) - Jan1st1970).TotalSeconds;
  106. }
  107. /// <summary>
  108. /// 时间戳转换为日期(时间戳单位秒)
  109. /// </summary>
  110. /// <param name="TimeStamp"></param>
  111. /// <returns></returns>
  112. public static DateTime ToDateTime(this long timeStamp)
  113. {
  114. var start = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
  115. return start.AddSeconds(timeStamp).AddHours(8);
  116. }
  117. public static WXUser CopyFromOAuthUserInfo(this WXUser user, OAuthUserInfo userInfo)
  118. {
  119. user.Nickname = userInfo.nickname;
  120. user.HeadimgUrl = userInfo.headimgurl;
  121. user.Province = userInfo.province;
  122. user.UnionId = userInfo.unionid;
  123. user.Sex = userInfo.sex;
  124. user.OpenId = userInfo.openid;
  125. return user;
  126. }
  127. }
  128. }