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

11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
10 years ago
11 years ago
10 years ago
11 years ago
11 years ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Reflection;
  4. using Shadowsocks.Encryption.AEAD;
  5. using Shadowsocks.Encryption.Stream;
  6. namespace Shadowsocks.Encryption
  7. {
  8. public static class EncryptorFactory
  9. {
  10. private static Dictionary<string, Type> _registeredEncryptors = new Dictionary<string, Type>();
  11. private static readonly Type[] ConstructorTypes = {typeof(string), typeof(string)};
  12. static EncryptorFactory()
  13. {
  14. foreach (string method in StreamMbedTLSEncryptor.SupportedCiphers())
  15. {
  16. _registeredEncryptors.Add(method, typeof(StreamMbedTLSEncryptor));
  17. }
  18. foreach (string method in StreamSodiumEncryptor.SupportedCiphers())
  19. {
  20. _registeredEncryptors.Add(method, typeof(StreamSodiumEncryptor));
  21. }
  22. foreach (string method in AEADMbedTLSEncryptor.SupportedCiphers())
  23. {
  24. _registeredEncryptors.Add(method, typeof(AEADMbedTLSEncryptor));
  25. }
  26. foreach (string method in AEADSodiumEncryptor.SupportedCiphers())
  27. {
  28. _registeredEncryptors.Add(method, typeof(AEADSodiumEncryptor));
  29. }
  30. }
  31. public static IEncryptor GetEncryptor(string method, string password)
  32. {
  33. if (method.IsNullOrEmpty())
  34. {
  35. method = "aes-256-cfb";
  36. }
  37. method = method.ToLowerInvariant();
  38. Type t = _registeredEncryptors[method];
  39. ConstructorInfo c = t.GetConstructor(ConstructorTypes);
  40. if (c == null) throw new System.Exception("Invalid ctor");
  41. IEncryptor result = (IEncryptor) c.Invoke(new object[] {method, password});
  42. return result;
  43. }
  44. }
  45. }