You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. using System;
  2. using System.Security.Cryptography;
  3. namespace Shadowsocks.Encryption
  4. {
  5. public static class RNG
  6. {
  7. private static RNGCryptoServiceProvider _rng = null;
  8. public static void Init()
  9. {
  10. if (_rng == null)
  11. _rng = new RNGCryptoServiceProvider();
  12. }
  13. public static void Close()
  14. {
  15. if (_rng == null) return;
  16. _rng.Dispose();
  17. _rng = null;
  18. }
  19. public static void Reload()
  20. {
  21. Close();
  22. Init();
  23. }
  24. public static void GetBytes(byte[] buf)
  25. {
  26. _rng.GetBytes(buf);
  27. }
  28. public static void GetBytes(byte[] buf, int len)
  29. {
  30. try
  31. {
  32. _rng.GetBytes(buf, 0, len);
  33. }
  34. catch (Exception)
  35. {
  36. // the backup way
  37. byte[] tmp = new byte[len];
  38. _rng.GetBytes(tmp);
  39. Buffer.BlockCopy(tmp, 0, buf, 0, len);
  40. }
  41. }
  42. }
  43. }