Browse Source

refine factory

tags/2.3
clowwindy 9 years ago
parent
commit
12047ca70b
4 changed files with 59 additions and 12 deletions
  1. +31
    -4
      shadowsocks-csharp/Encrypt/EncryptorFactory.cs
  2. +11
    -4
      shadowsocks-csharp/Encrypt/PolarSSLEncryptor.cs
  3. +11
    -4
      shadowsocks-csharp/Encrypt/SodiumEncryptor.cs
  4. +6
    -0
      shadowsocks-csharp/Encrypt/TableEncryptor.cs

+ 31
- 4
shadowsocks-csharp/Encrypt/EncryptorFactory.cs View File

@@ -1,16 +1,43 @@

using System;
using System.Collections.Generic;
using System.Reflection;
namespace Shadowsocks.Encrypt
{
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;
}
}
}

+ 11
- 4
shadowsocks-csharp/Encrypt/PolarSSLEncryptor.cs View File

@@ -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
shadowsocks-csharp/Encrypt/SodiumEncryptor.cs 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)


+ 6
- 0
shadowsocks-csharp/Encrypt/TableEncryptor.cs View File

@@ -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];


Loading…
Cancel
Save