| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167 |
- 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<T>()
- {
- 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<T>();
- }
- 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;
- }
- /// <summary>
- /// 日期转换为时间戳(时间戳单位秒)
- /// </summary>
- /// <param name="TimeStamp"></param>
- /// <returns></returns>
- 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;
- }
- /// <summary>
- /// 时间戳转换为日期(时间戳单位秒)
- /// </summary>
- /// <param name="TimeStamp"></param>
- /// <returns></returns>
- 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;
- }
- }
- }
|