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.

EncryptorFactory.cs 1.6 kB

10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. 
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Reflection;
  5. namespace Shadowsocks.Encryption
  6. {
  7. public static class EncryptorFactory
  8. {
  9. private static Dictionary<string, Type> _registeredEncryptors;
  10. private static Type[] _constructorTypes = new Type[] { typeof(string), typeof(string), typeof(bool) };
  11. static EncryptorFactory()
  12. {
  13. _registeredEncryptors = new Dictionary<string, Type>();
  14. foreach (string method in TableEncryptor.SupportedCiphers())
  15. {
  16. _registeredEncryptors.Add(method, typeof(TableEncryptor));
  17. }
  18. foreach (string method in PolarSSLEncryptor.SupportedCiphers())
  19. {
  20. _registeredEncryptors.Add(method, typeof(PolarSSLEncryptor));
  21. }
  22. foreach (string method in SodiumEncryptor.SupportedCiphers())
  23. {
  24. _registeredEncryptors.Add(method, typeof(SodiumEncryptor));
  25. }
  26. }
  27. public static IEncryptor GetEncryptor(string method, string password, bool onetimeauth = false)
  28. {
  29. if (string.IsNullOrEmpty(method))
  30. {
  31. method = "table";
  32. }
  33. method = method.ToLowerInvariant();
  34. Type t = _registeredEncryptors[method];
  35. ConstructorInfo c = t.GetConstructor(_constructorTypes);
  36. IEncryptor result = (IEncryptor)c.Invoke(new object[] { method, password, onetimeauth });
  37. return result;
  38. }
  39. }
  40. }