@@ -1,16 +1,43 @@ | |||||
| | ||||
using System; | |||||
using System.Collections.Generic; | |||||
using System.Reflection; | |||||
namespace Shadowsocks.Encrypt | namespace Shadowsocks.Encrypt | ||||
{ | { | ||||
public static class EncryptorFactory | public static class EncryptorFactory | ||||
{ | { | ||||
public static IEncryptor GetEncryptor(string method, string password) | |||||
private static Dictionary<string, Type> _registeredEncryptors; | |||||
private static Type[] _constructorTypes = new Type[] { typeof(string), typeof(string) }; | |||||
static EncryptorFactory() | |||||
{ | { | ||||
if (string.IsNullOrEmpty(method) || method.ToLowerInvariant() == "table") | |||||
_registeredEncryptors = new Dictionary<string, Type>(); | |||||
foreach (string method in TableEncryptor.SupportedCiphers()) | |||||
{ | |||||
_registeredEncryptors.Add(method, typeof(TableEncryptor)); | |||||
} | |||||
foreach (string method in PolarSSLEncryptor.SupportedCiphers()) | |||||
{ | |||||
_registeredEncryptors.Add(method, typeof(PolarSSLEncryptor)); | |||||
} | |||||
foreach (string method in SodiumEncryptor.SupportedCiphers()) | |||||
{ | { | ||||
return new TableEncryptor(method, password); | |||||
_registeredEncryptors.Add(method, typeof(SodiumEncryptor)); | |||||
} | } | ||||
} | |||||
return new SodiumEncryptor(method, password); | |||||
public static IEncryptor GetEncryptor(string method, string password) | |||||
{ | |||||
if (string.IsNullOrEmpty(method)) | |||||
{ | |||||
method = "table"; | |||||
} | |||||
method = method.ToLowerInvariant(); | |||||
Type t = _registeredEncryptors[method]; | |||||
ConstructorInfo c = t.GetConstructor(_constructorTypes); | |||||
IEncryptor result = (IEncryptor)c.Invoke(new object[] { method, password }); | |||||
return result; | |||||
} | } | ||||
} | } | ||||
} | } |
@@ -22,15 +22,22 @@ namespace Shadowsocks.Encrypt | |||||
InitKey(method, password); | InitKey(method, password); | ||||
} | } | ||||
protected override Dictionary<string, int[]> getCiphers() | |||||
{ | |||||
return new Dictionary<string, int[]> { | |||||
private static Dictionary<string, int[]> _ciphers = new Dictionary<string, int[]> { | |||||
{"aes-128-cfb", new int[]{16, 16, CIPHER_AES, PolarSSL.AES_CTX_SIZE}}, | {"aes-128-cfb", new int[]{16, 16, CIPHER_AES, PolarSSL.AES_CTX_SIZE}}, | ||||
{"aes-192-cfb", new int[]{24, 16, CIPHER_AES, PolarSSL.AES_CTX_SIZE}}, | {"aes-192-cfb", new int[]{24, 16, CIPHER_AES, PolarSSL.AES_CTX_SIZE}}, | ||||
{"aes-256-cfb", new int[]{32, 16, CIPHER_AES, PolarSSL.AES_CTX_SIZE}}, | {"aes-256-cfb", new int[]{32, 16, CIPHER_AES, PolarSSL.AES_CTX_SIZE}}, | ||||
{"rc4", new int[]{16, 0, CIPHER_RC4, PolarSSL.ARC4_CTX_SIZE}}, | {"rc4", new int[]{16, 0, CIPHER_RC4, PolarSSL.ARC4_CTX_SIZE}}, | ||||
{"rc4-md5", new int[]{16, 16, CIPHER_RC4, PolarSSL.ARC4_CTX_SIZE}}, | {"rc4-md5", new int[]{16, 16, CIPHER_RC4, PolarSSL.ARC4_CTX_SIZE}}, | ||||
}; | |||||
}; | |||||
public static List<string> SupportedCiphers() | |||||
{ | |||||
return new List<string>(_ciphers.Keys); | |||||
} | |||||
protected override Dictionary<string, int[]> getCiphers() | |||||
{ | |||||
return _ciphers; | |||||
} | } | ||||
protected override void initCipher(byte[] iv, bool isCipher) | protected override void initCipher(byte[] iv, bool isCipher) | ||||
@@ -27,12 +27,19 @@ namespace Shadowsocks.Encrypt | |||||
_decryptBuf = new byte[MAX_INPUT_SIZE + SODIUM_BLOCK_SIZE]; | _decryptBuf = new byte[MAX_INPUT_SIZE + SODIUM_BLOCK_SIZE]; | ||||
} | } | ||||
protected override Dictionary<string, int[]> getCiphers() | |||||
{ | |||||
return new Dictionary<string, int[]> { | |||||
private static Dictionary<string, int[]> _ciphers = new Dictionary<string, int[]> { | |||||
{"salsa20", new int[]{32, 8, CIPHER_SALSA20, PolarSSL.AES_CTX_SIZE}}, | {"salsa20", new int[]{32, 8, CIPHER_SALSA20, PolarSSL.AES_CTX_SIZE}}, | ||||
{"chacha20", new int[]{32, 8, CIPHER_CHACHA20, PolarSSL.AES_CTX_SIZE}}, | {"chacha20", new int[]{32, 8, CIPHER_CHACHA20, PolarSSL.AES_CTX_SIZE}}, | ||||
}; ; | |||||
}; | |||||
protected override Dictionary<string, int[]> getCiphers() | |||||
{ | |||||
return _ciphers; | |||||
} | |||||
public static List<string> SupportedCiphers() | |||||
{ | |||||
return new List<string>(_ciphers.Keys); | |||||
} | } | ||||
protected override void cipherUpdate(bool isCipher, int length, byte[] buf, byte[] outbuf) | protected override void cipherUpdate(bool isCipher, int length, byte[] buf, byte[] outbuf) | ||||
@@ -1,4 +1,5 @@ | |||||
using System; | using System; | ||||
using System.Collections.Generic; | |||||
namespace Shadowsocks.Encrypt | namespace Shadowsocks.Encrypt | ||||
{ | { | ||||
@@ -25,6 +26,11 @@ namespace Shadowsocks.Encrypt | |||||
} | } | ||||
} | } | ||||
public static List<string> SupportedCiphers() | |||||
{ | |||||
return new List<string>(new string[]{"table"}); | |||||
} | |||||
public override void Encrypt(byte[] buf, int length, byte[] outbuf, out int outlength) | public override void Encrypt(byte[] buf, int length, byte[] outbuf, out int outlength) | ||||
{ | { | ||||
byte[] result = new byte[length]; | byte[] result = new byte[length]; | ||||