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.4 kB

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