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.

CryptoFactory.cs 3.6 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Reflection;
  4. using System.Text;
  5. using Shadowsocks.Net.Crypto.AEAD;
  6. using Shadowsocks.Net.Crypto.Stream;
  7. namespace Shadowsocks.Net.Crypto
  8. {
  9. public static class CryptoFactory
  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 CryptoFactory()
  16. {
  17. foreach (var method in StreamPlainNativeCrypto.SupportedCiphers())
  18. {
  19. if (!_registeredEncryptors.ContainsKey(method.Key))
  20. {
  21. ciphers.Add(method.Key, method.Value);
  22. _registeredEncryptors.Add(method.Key, typeof(StreamPlainNativeCrypto));
  23. }
  24. }
  25. foreach (var method in StreamCryptoBaseCrypto.SupportedCiphers())
  26. {
  27. if (!_registeredEncryptors.ContainsKey(method.Key))
  28. {
  29. ciphers.Add(method.Key, method.Value);
  30. _registeredEncryptors.Add(method.Key, typeof(StreamCryptoBaseCrypto));
  31. }
  32. }
  33. foreach (var method in AEADCryptoBaseCrypto.SupportedCiphers())
  34. {
  35. if (!_registeredEncryptors.ContainsKey(method.Key))
  36. {
  37. ciphers.Add(method.Key, method.Value);
  38. _registeredEncryptors.Add(method.Key, typeof(AEADCryptoBaseCrypto));
  39. }
  40. }
  41. }
  42. public static ICrypto GetEncryptor(string method, string password)
  43. {
  44. if (string.IsNullOrEmpty(method))
  45. {
  46. // todo
  47. //method = IoCManager.Container.Resolve<IDefaultCrypto>().GetDefaultMethod();
  48. }
  49. method = method.ToLowerInvariant();
  50. bool ok = _registeredEncryptors.TryGetValue(method, out Type t);
  51. if (!ok)
  52. {
  53. t = _registeredEncryptors[DefaultCipher];
  54. }
  55. ConstructorInfo c = t?.GetConstructor(ConstructorTypes) ??
  56. throw new TypeLoadException("can't load constructor");
  57. if (c == null) throw new System.Exception("Invalid ctor");
  58. ICrypto result = (ICrypto)c.Invoke(new object[] { method, password });
  59. return result;
  60. }
  61. public static string DumpRegisteredEncryptor()
  62. {
  63. var sb = new StringBuilder();
  64. sb.Append(Environment.NewLine);
  65. sb.AppendLine("-------------------------");
  66. sb.AppendLine("Registered Encryptor Info");
  67. foreach (var encryptor in _registeredEncryptors)
  68. {
  69. sb.AppendLine($"{ciphers[encryptor.Key].ToString(true)} => {encryptor.Value.Name}");
  70. }
  71. // use ----- instead of =======, so when user paste it to Github, it won't became title
  72. sb.AppendLine("-------------------------");
  73. return sb.ToString();
  74. }
  75. public static CipherInfo GetCipherInfo(string name)
  76. {
  77. // TODO: Replace cipher when required not exist
  78. return ciphers[name];
  79. }
  80. public static IEnumerable<CipherInfo> ListAvaliableCiphers()
  81. {
  82. return ciphers.Values;
  83. }
  84. }
  85. }