Utils.cs 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. using Microsoft.AspNetCore.Cryptography.KeyDerivation;
  2. using Microsoft.IdentityModel.Tokens;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Drawing;
  6. using System.Drawing.Drawing2D;
  7. using System.Drawing.Imaging;
  8. using System.IdentityModel.Tokens.Jwt;
  9. using System.IO;
  10. using System.Security.Claims;
  11. using System.Security.Cryptography;
  12. using System.Text;
  13. namespace TEAMModelOS.SDK.Extension
  14. {
  15. public class Utils
  16. {
  17. private static RNGCryptoServiceProvider _random = new RNGCryptoServiceProvider();
  18. public static string HashedPassword(string password, string salt)
  19. {
  20. byte[] hashBytes = KeyDerivation.Pbkdf2(
  21. password: password,
  22. salt: Encoding.UTF8.GetBytes(salt), //SHA1鹽(8-20字節),SHA256(32字節)
  23. prf: KeyDerivationPrf.HMACSHA1,
  24. iterationCount: 10000, //hash次數,越多次代表破解難度變高,但效能會差點
  25. numBytesRequested: 256 / 8 //指定得出結果長度
  26. );
  27. string hashText = BitConverter.ToString(hashBytes).Replace("-", string.Empty);
  28. return hashText;
  29. }
  30. /// <summary>
  31. /// 建立真隨機字串(CSPRNG),適用密碼、鹽
  32. /// </summary>
  33. /// <param name="stringLength">長度</param>
  34. /// <param name="key">要限制的字元串(長度需大於等於8),如果為null或者小於8,預設為"abcdefghijklmnopqrstuvwxyz1234567890"</param>
  35. /// <returns></returns>
  36. public static string CreatSaltString(int stringLength, string key = null)
  37. {
  38. ReadOnlySpan<char> span;
  39. if (key == null || key.Length < 8)
  40. span = "abcdefghijklmnopqrstuvwxyz1234567890";
  41. else
  42. span = key.AsSpan();
  43. int length = span.Length;
  44. StringBuilder randomString = new StringBuilder(length);
  45. for (int i = 0; i < stringLength; ++i)
  46. {
  47. randomString.Append(span[SetRandomSeeds(length)]);
  48. }
  49. return randomString.ToString();
  50. }
  51. /// <summary>
  52. /// 建立真隨機整數數字(CSPRNG),適用亂數、隨機編號
  53. /// </summary>
  54. /// <param name="max">最大值</param>
  55. public static int CreatSaltInt(int max)
  56. {
  57. var bytes = new byte[4];
  58. _random.GetBytes(bytes);
  59. int value = BitConverter.ToInt32(bytes, 0);
  60. value = value % (max + 1);
  61. if (value < 0) value = -value;
  62. return value;
  63. }
  64. /// <summary>
  65. /// 建立真隨機整數數字(CSPRNG),適用亂數、隨機編號
  66. /// </summary>
  67. /// <param name="min">最小值</param>
  68. /// <param name="max">最大值</param>
  69. public static int CreatSaltInt(int min, int max)
  70. {
  71. int value = CreatSaltInt(max - min) + min;
  72. return value;
  73. }
  74. /// <summary>
  75. /// 剖析連接字串
  76. /// </summary>
  77. /// <param name="connectionString"></param>
  78. /// <returns></returns>
  79. public static Dictionary<string, string> ParseConnectionString(string connectionString)
  80. {
  81. var d = new Dictionary<string, string>();
  82. foreach (var item in connectionString.Split(';', StringSplitOptions.RemoveEmptyEntries))
  83. {
  84. var a = item.IndexOf('=');
  85. d.Add(item.Substring(0, a), item.Substring(a + 1));
  86. }
  87. return d;
  88. }
  89. #region 圖片處理
  90. /// <summary>
  91. /// 判斷圖片格式
  92. /// </summary>
  93. /// <param name="stream">傳入Stream,注意請自行釋放</param>
  94. /// <returns></returns>
  95. public static (bool, string, int) ImageValidateByStream(Stream stream)
  96. {
  97. int length = 1024;
  98. byte[] bytes = new byte[1024];
  99. BinaryReader br = new BinaryReader(stream);
  100. StringBuilder stringBuilder = new StringBuilder();
  101. for (int i = 0; i < length; i++)
  102. {
  103. byte tempByte = br.ReadByte();
  104. bytes[i] = tempByte;
  105. stringBuilder.Append(Convert.ToString(tempByte, 16));
  106. stringBuilder.Append(",");
  107. }
  108. stream.Position = 0; //指針回歸為0
  109. string fileheader = stringBuilder.ToString().ToUpper();
  110. if (string.IsNullOrWhiteSpace(fileheader))
  111. return (false, "", 0);
  112. if (fileheader.StartsWith("FF,D8,") || fileheader.StartsWith("42,4D,"))
  113. {
  114. int i = 2;
  115. int depth;
  116. while (true)
  117. {
  118. int marker = (bytes[i] & 0xff) << 8 | (bytes[i + 1] & 0xff);
  119. int size = (bytes[i + 2] & 0xff) << 8 | (bytes[i + 3] & 0xff);
  120. if (marker >= 0xffc0 && marker <= 0xffcf && marker != 0xffc4 && marker != 0xffc8)
  121. {
  122. depth = (bytes[i + 4] & 0xff) * (bytes[i + 9] & 0xff);
  123. break;
  124. }
  125. else i += size + 2;
  126. }
  127. return (true, "jpg", depth);
  128. }
  129. if (fileheader.StartsWith("89,50,4E,47,D,A,1A,A,"))
  130. {
  131. int depth = bytes[24] & 0xff;
  132. if ((bytes[25] & 0xff) == 2) depth *= 3;
  133. else if ((bytes[25] & 0xff) == 6) depth *= 4;
  134. return (true, "png", depth);
  135. }
  136. if (fileheader.StartsWith("47,49,46,38,39,61,") || fileheader.StartsWith("47,49,46,38,37,61,"))
  137. return (true, "gif", 0);
  138. if (fileheader.StartsWith("1,0,0,0,"))
  139. return (true, "emf", 0);
  140. if (fileheader.StartsWith("1,0,9,0,0,3"))
  141. return (true, "wmf", 0);
  142. //if (fileheader.StartsWith("4D,4D") || fileheader.StartsWith("49,49") || fileheader.StartsWith("46,4F,52,4D"))
  143. // return (true, "tif");
  144. return (false, "", 0);
  145. }
  146. /// <summary>
  147. /// 轉換EMF為PNG格式
  148. /// </summary>
  149. /// <param name="stream"></param>
  150. /// <returns></returns>
  151. public static string ConvertEMFtoPNG(Stream stream)
  152. {
  153. using var image = Image.FromStream(stream);
  154. //GDI+報錯,Azure Web App沙箱不支持GDI+部分權限
  155. //using var nbase64ms = new MemoryStream();
  156. //image.Save(nbase64ms, ImageFormat.Png); //保存為PNG格式
  157. //byte[] data = nbase64ms.ToArray();
  158. //string base64 = Convert.ToBase64String(data);
  159. //return base64;
  160. //Azure Web App沙箱不支持GDI+渲染部分EMF指令,可以出圖但會是空白
  161. var pngimage = new Bitmap(image.Width, image.Height);
  162. using (var graphics = Graphics.FromImage(pngimage))
  163. {
  164. graphics.CompositingQuality = CompositingQuality.HighSpeed;
  165. graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
  166. graphics.CompositingMode = CompositingMode.SourceCopy;
  167. graphics.DrawImage(image, 0, 0, image.Width, image.Height);
  168. using var nbase64ms = new MemoryStream();
  169. pngimage.Save(nbase64ms, ImageFormat.Png); //保存為PNG格式
  170. byte[] data = nbase64ms.ToArray();
  171. string base64 = Convert.ToBase64String(data);
  172. return base64;
  173. }
  174. }
  175. #endregion
  176. #region private
  177. private static int SetRandomSeeds(int length)
  178. {
  179. decimal maxValue = (decimal)long.MaxValue;
  180. byte[] array = new byte[8];
  181. _random.GetBytes(array);
  182. return (int)(Math.Abs(BitConverter.ToInt64(array, 0)) / maxValue * length);
  183. }
  184. #endregion
  185. }
  186. }