Files
shadowsocks-windows/shadowsocks-csharp/Encryption/EncryptorFactory.cs
T
Syrone Wong 2fafc52def Refine encryption(#655)
* Refine encryption

- Add AES-CTR, blowfish and camellia ciphers

aes-256-ctr
aes-192-ctr
aes-128-ctr
bf-cfb
camellia-128-cfb
camellia-192-cfb
camellia-256-cfb

- Merge the previous PolarSSL and MbedTLS
- Switch to MbedTLS's cipher layer functions
- Add workaround to set cipher operation(encrypt/decrypt)

Signed-off-by: Syrone Wong <wong.syrone@gmail.com>

* Add function to get MbedTLS cipher context size

and drop hard-coded ctx sizes

Signed-off-by: Syrone Wong <wong.syrone@gmail.com>
2016-08-13 14:28:09 +08:00

40 lines
1.4 KiB
C#

using System;
using System.Collections.Generic;
using System.Reflection;
namespace Shadowsocks.Encryption
{
public static class EncryptorFactory
{
private static Dictionary<string, Type> _registeredEncryptors;
private static Type[] _constructorTypes = new Type[] { typeof(string), typeof(string), typeof(bool), typeof(bool) };
static EncryptorFactory()
{
_registeredEncryptors = new Dictionary<string, Type>();
foreach (string method in MbedTLSEncryptor.SupportedCiphers())
{
_registeredEncryptors.Add(method, typeof(MbedTLSEncryptor));
}
foreach (string method in SodiumEncryptor.SupportedCiphers())
{
_registeredEncryptors.Add(method, typeof(SodiumEncryptor));
}
}
public static IEncryptor GetEncryptor(string method, string password, bool onetimeauth, bool isudp)
{
if (method.IsNullOrEmpty())
{
method = "aes-256-cfb";
}
method = method.ToLowerInvariant();
Type t = _registeredEncryptors[method];
ConstructorInfo c = t.GetConstructor(_constructorTypes);
IEncryptor result = (IEncryptor)c.Invoke(new object[] { method, password, onetimeauth, isudp });
return result;
}
}
}