using Microsoft.Extensions.Logging; using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using System.Security.Cryptography; using System.IO; using System.Text; using Winsoft.GOV.XF.WebApi.WXCore.Models; using Senparc.Weixin.MP.AdvancedAPIs.OAuth; namespace Winsoft.GOV.XF.WebApi.WXCore.Helpers { public static class Utilities { static ILoggerFactory _loggerFactory; public static void ConfigureLogger(ILoggerFactory loggerFactory) { _loggerFactory = loggerFactory; } public static ILogger CreateLogger() { if (_loggerFactory == null) { throw new InvalidOperationException($"{nameof(ILogger)} is not configured. {nameof(ConfigureLogger)} must be called before use"); //_loggerFactory = new LoggerFactory().AddConsole().AddDebug(); } return _loggerFactory.CreateLogger(); } public static string ContentRootPath { get; set; } public static string AESEncrypt(this string input, string key) { byte[] bytesToBeEncrypted = Encoding.UTF8.GetBytes(input); byte[] passwordBytes = Encoding.UTF8.GetBytes(key); passwordBytes = SHA256.Create().ComputeHash(passwordBytes); byte[] bytesEncrypted = AESEncryptBytes(bytesToBeEncrypted, passwordBytes); string result = Convert.ToBase64String(bytesEncrypted); return result; } private static byte[] AESEncryptBytes(byte[] bytesToBeEncrypted, byte[] passwordBytes) { byte[] encryptedBytes = null; var saltBytes = new byte[9] { 13, 34, 27, 67, 189, 255, 104, 219, 122 }; using (var ms = new MemoryStream()) { using (var AES = new RijndaelManaged()) { AES.KeySize = 256; AES.BlockSize = 128; var key = new Rfc2898DeriveBytes(passwordBytes, saltBytes, 1000); AES.Key = key.GetBytes(32); AES.IV = key.GetBytes(16); AES.Mode = CipherMode.CBC; using (var cs = new CryptoStream(ms, AES.CreateEncryptor(), CryptoStreamMode.Write)) { cs.Write(bytesToBeEncrypted, 0, bytesToBeEncrypted.Length); cs.Close(); } encryptedBytes = ms.ToArray(); } } return encryptedBytes; } public static string AESDecrypt(this string input, string key) { byte[] bytesToBeDecrypted = Convert.FromBase64String(input); byte[] passwordBytes = Encoding.UTF8.GetBytes(key); passwordBytes = SHA256.Create().ComputeHash(passwordBytes); byte[] bytesDecrypted = AESDecryptBytes(bytesToBeDecrypted, passwordBytes); string result = Encoding.UTF8.GetString(bytesDecrypted); return result; } public static byte[] AESDecryptBytes(byte[] bytesToBeDecrypted, byte[] passwordBytes) { byte[] decryptedBytes = null; var saltBytes = new byte[9] { 13, 34, 27, 67, 189, 255, 104, 219, 122 }; using (var ms = new MemoryStream()) { using (var AES = new RijndaelManaged()) { AES.KeySize = 256; AES.BlockSize = 128; var key = new Rfc2898DeriveBytes(passwordBytes, saltBytes, 1000); AES.Key = key.GetBytes(32); AES.IV = key.GetBytes(16); AES.Mode = CipherMode.CBC; using (var cs = new CryptoStream(ms, AES.CreateDecryptor(), CryptoStreamMode.Write)) { cs.Write(bytesToBeDecrypted, 0, bytesToBeDecrypted.Length); cs.Close(); } decryptedBytes = ms.ToArray(); } } return decryptedBytes; } /// /// 日期转换为时间戳(时间戳单位秒) /// /// /// public static long ToTimeStamp(this DateTime time) { DateTime Jan1st1970 = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc); return (long) (time.AddHours(-8) - Jan1st1970).TotalSeconds; } /// /// 时间戳转换为日期(时间戳单位秒) /// /// /// public static DateTime ToDateTime(this long timeStamp) { var start = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc); return start.AddSeconds(timeStamp).AddHours(8); } public static WXUser CopyFromOAuthUserInfo(this WXUser user, OAuthUserInfo userInfo) { user.Nickname = userInfo.nickname; user.HeadimgUrl = userInfo.headimgurl; user.Province = userInfo.province; user.UnionId = userInfo.unionid; user.Sex = userInfo.sex; user.OpenId = userInfo.openid; return user; } } }