Utils.cs 3.5 KB

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