Utils.cs 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. using Microsoft.AspNetCore.Cryptography.KeyDerivation;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Security.Cryptography;
  5. using System.Text;
  6. namespace TEAMModelOS.SDK.Extension
  7. {
  8. public class Utils
  9. {
  10. private static RNGCryptoServiceProvider _random = new RNGCryptoServiceProvider();
  11. public static string HashedPassword(string password, string salt)
  12. {
  13. byte[] hashBytes = KeyDerivation.Pbkdf2(
  14. password: password,
  15. salt: Encoding.UTF8.GetBytes(salt), //SHA1鹽(8-20字節),SHA256(32字節)
  16. prf: KeyDerivationPrf.HMACSHA1,
  17. iterationCount: 10000, //hash次數,越多次代表破解難度變高,但效能會差點
  18. numBytesRequested: 256 / 8 //指定得出結果長度
  19. );
  20. string hashText = BitConverter.ToString(hashBytes).Replace("-", string.Empty);
  21. return hashText;
  22. }
  23. /// <summary>
  24. /// 建立真隨機字串(CSPRNG),適用密碼、鹽
  25. /// </summary>
  26. /// <param name="stringLength">長度</param>
  27. /// <param name="key">要限制的字元串(長度需大於等於8),如果為null或者小於8,預設為"abcdefghijklmnopqrstuvwxyz1234567890"</param>
  28. /// <returns></returns>
  29. public static string CreatSaltString(int stringLength, string key = null)
  30. {
  31. ReadOnlySpan<char> span;
  32. if (key == null || key.Length < 8)
  33. span = "abcdefghijklmnopqrstuvwxyz1234567890";
  34. else
  35. span = key.AsSpan();
  36. int length = span.Length;
  37. StringBuilder randomString = new StringBuilder(length);
  38. for (int i = 0; i < stringLength; ++i)
  39. {
  40. randomString.Append(span[SetRandomSeeds(length)]);
  41. }
  42. return randomString.ToString();
  43. }
  44. /// <summary>
  45. /// 建立真隨機整數數字(CSPRNG),適用亂數、隨機編號
  46. /// </summary>
  47. /// <param name="max">最大值</param>
  48. public static int CreatSaltInt(int max)
  49. {
  50. var bytes = new byte[4];
  51. _random.GetBytes(bytes);
  52. int value = BitConverter.ToInt32(bytes, 0);
  53. value = value % (max + 1);
  54. if (value < 0) value = -value;
  55. return value;
  56. }
  57. /// <summary>
  58. /// 建立真隨機整數數字(CSPRNG),適用亂數、隨機編號
  59. /// </summary>
  60. /// <param name="min">最小值</param>
  61. /// <param name="max">最大值</param>
  62. public static int CreatSaltInt(int min, int max)
  63. {
  64. int value = CreatSaltInt(max - min) + min;
  65. return value;
  66. }
  67. public static Dictionary<string, string> ParseConnectionString(string connectionString)
  68. {
  69. var d = new Dictionary<string, string>();
  70. foreach (var item in connectionString.Split(';',StringSplitOptions.RemoveEmptyEntries))
  71. {
  72. var a = item.IndexOf('=');
  73. d.Add(item.Substring(0, a), item.Substring(a + 1));
  74. }
  75. return d;
  76. }
  77. private static int SetRandomSeeds(int length)
  78. {
  79. decimal maxValue = (decimal)long.MaxValue;
  80. byte[] array = new byte[8];
  81. _random.GetBytes(array);
  82. return (int)(Math.Abs(BitConverter.ToInt64(array, 0)) / maxValue * length);
  83. }
  84. }
  85. }