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 4.8 kB

11 years ago
11 years ago
11 years ago
11 years ago
10 years ago
11 years ago
11 years ago
11 years ago
11 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Reflection;
  4. using System.Text;
  5. using Shadowsocks.Encryption.AEAD;
  6. using Shadowsocks.Encryption.Stream;
  7. namespace Shadowsocks.Encryption
  8. {
  9. public static class EncryptorFactory
  10. {
  11. public static string DefaultCipher = "chacha20-ietf-poly1305";
  12. private static readonly Dictionary<string, Type> _registeredEncryptors = new Dictionary<string, Type>();
  13. private static readonly Dictionary<string, CipherInfo> ciphers = new Dictionary<string, CipherInfo>();
  14. private static readonly Type[] ConstructorTypes = { typeof(string), typeof(string) };
  15. static EncryptorFactory()
  16. {
  17. foreach (var method in StreamPlainNativeEncryptor.SupportedCiphers())
  18. {
  19. if (!_registeredEncryptors.ContainsKey(method.Key))
  20. {
  21. ciphers.Add(method.Key, method.Value);
  22. _registeredEncryptors.Add(method.Key, typeof(StreamPlainNativeEncryptor));
  23. }
  24. }
  25. foreach (var method in StreamRc4NativeEncryptor.SupportedCiphers())
  26. {
  27. if (!_registeredEncryptors.ContainsKey(method.Key))
  28. {
  29. ciphers.Add(method.Key, method.Value);
  30. _registeredEncryptors.Add(method.Key, typeof(StreamRc4NativeEncryptor));
  31. }
  32. }
  33. foreach (var method in StreamAesCfbBouncyCastleEncryptor.SupportedCiphers())
  34. {
  35. if (!_registeredEncryptors.ContainsKey(method.Key))
  36. {
  37. ciphers.Add(method.Key, method.Value);
  38. _registeredEncryptors.Add(method.Key, typeof(StreamAesCfbBouncyCastleEncryptor));
  39. }
  40. }
  41. foreach (var method in StreamChachaNaClEncryptor.SupportedCiphers())
  42. {
  43. if (!_registeredEncryptors.ContainsKey(method.Key))
  44. {
  45. ciphers.Add(method.Key, method.Value);
  46. _registeredEncryptors.Add(method.Key, typeof(StreamChachaNaClEncryptor));
  47. }
  48. }
  49. foreach (var method in AEADAesGcmNativeEncryptor.SupportedCiphers())
  50. {
  51. if (!_registeredEncryptors.ContainsKey(method.Key))
  52. {
  53. ciphers.Add(method.Key, method.Value);
  54. _registeredEncryptors.Add(method.Key, typeof(AEADAesGcmNativeEncryptor));
  55. }
  56. }
  57. foreach (var method in AEADNaClEncryptor.SupportedCiphers())
  58. {
  59. if (!_registeredEncryptors.ContainsKey(method.Key))
  60. {
  61. ciphers.Add(method.Key, method.Value);
  62. _registeredEncryptors.Add(method.Key, typeof(AEADNaClEncryptor));
  63. }
  64. }
  65. }
  66. public static IEncryptor GetEncryptor(string method, string password)
  67. {
  68. if (method.IsNullOrEmpty())
  69. {
  70. method = Model.Server.DefaultMethod;
  71. }
  72. method = method.ToLowerInvariant();
  73. bool ok = _registeredEncryptors.TryGetValue(method, out Type t);
  74. if (!ok)
  75. {
  76. t = _registeredEncryptors[DefaultCipher];
  77. }
  78. ConstructorInfo c = t?.GetConstructor(ConstructorTypes) ??
  79. throw new TypeLoadException("can't load constructor");
  80. if (c == null) throw new System.Exception("Invalid ctor");
  81. IEncryptor result = (IEncryptor)c.Invoke(new object[] { method, password });
  82. return result;
  83. }
  84. public static string DumpRegisteredEncryptor()
  85. {
  86. var sb = new StringBuilder();
  87. sb.Append(Environment.NewLine);
  88. sb.AppendLine("-------------------------");
  89. sb.AppendLine("Registered Encryptor Info");
  90. foreach (var encryptor in _registeredEncryptors)
  91. {
  92. sb.AppendLine($"{ciphers[encryptor.Key].ToString(true)} => {encryptor.Value.Name}");
  93. }
  94. // use ----- instead of =======, so when user paste it to Github, it won't became title
  95. sb.AppendLine("-------------------------");
  96. return sb.ToString();
  97. }
  98. public static CipherInfo GetCipherInfo(string name)
  99. {
  100. // TODO: Replace cipher when required not exist
  101. return ciphers[name];
  102. }
  103. public static IEnumerable<CipherInfo> ListAvaliableCiphers()
  104. {
  105. return ciphers.Values;
  106. }
  107. }
  108. }