refine factory

This commit is contained in:
clowwindy
2014-12-13 10:40:42 +08:00
parent f0920098ad
commit 12047ca70b
4 changed files with 59 additions and 12 deletions
+31 -4
View File
@@ -1,16 +1,43 @@
using System;
using System.Collections.Generic;
using System.Reflection;
namespace Shadowsocks.Encrypt
{
public static class EncryptorFactory
{
private static Dictionary<string, Type> _registeredEncryptors;
private static Type[] _constructorTypes = new Type[] { typeof(string), typeof(string) };
static EncryptorFactory()
{
_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())
{
_registeredEncryptors.Add(method, typeof(SodiumEncryptor));
}
}
public static IEncryptor GetEncryptor(string method, string password)
{
if (string.IsNullOrEmpty(method) || method.ToLowerInvariant() == "table")
if (string.IsNullOrEmpty(method))
{
return new TableEncryptor(method, password);
method = "table";
}
return new SodiumEncryptor(method, password);
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);
}
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-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}},
{"rc4", new int[]{16, 0, 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)
+11 -4
View File
@@ -27,12 +27,19 @@ namespace Shadowsocks.Encrypt
_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}},
{"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)
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
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)
{
byte[] result = new byte[length];