private Func <SymmetricAlgorithm> GetSymmetricBlockCipherAlgorithmFactory(ManagedAuthenticatedEncryptorConfiguration configuration) { // basic argument checking if (configuration.EncryptionAlgorithmType == null) { throw Error.Common_PropertyCannotBeNullOrEmpty(nameof(configuration.EncryptionAlgorithmType)); } typeof(SymmetricAlgorithm).AssertIsAssignableFrom(configuration.EncryptionAlgorithmType); if (configuration.EncryptionAlgorithmKeySize < 0) { throw Error.Common_PropertyMustBeNonNegative(nameof(configuration.EncryptionAlgorithmKeySize)); } _logger.UsingManagedSymmetricAlgorithm(configuration.EncryptionAlgorithmType.FullName !); if (configuration.EncryptionAlgorithmType == typeof(Aes)) { Func <Aes>?factory = null; if (OSVersionUtil.IsWindows()) { // If we're on desktop CLR and running on Windows, use the FIPS-compliant implementation. factory = () => new AesCryptoServiceProvider(); } return(factory ?? Aes.Create); } else { return(AlgorithmActivator.CreateFactory <SymmetricAlgorithm>(configuration.EncryptionAlgorithmType)); } }
public void TestCanActivate_NullDefinition() { AlgorithmActivator activator = new AlgorithmActivator(new TestRegistrar()); bool canActivate = activator.CanActivate(null); Assert.IsFalse(canActivate); }
public void TestActivate_Interpreter_Exception() { AlgorithmActivator activator = new AlgorithmActivator(new InterpreterRegistrar()); AlgorithmDefinition d = new AlgorithmDefinition("Test", new Property[] { new Property("throw", typeof(double)) }); AlgorithmPlugin p = activator.Activate(d); }
private Func <KeyedHashAlgorithm> GetKeyedHashAlgorithmFactory(ILogger logger) { // basic argument checking if (ValidationAlgorithmType == null) { throw Error.Common_PropertyCannotBeNullOrEmpty(nameof(ValidationAlgorithmType)); } if (logger.IsVerboseLevelEnabled()) { logger.LogVerboseF($"Using managed keyed hash algorithm '{ValidationAlgorithmType.FullName}'."); } if (ValidationAlgorithmType == typeof(HMACSHA256)) { return(() => new HMACSHA256()); } else if (ValidationAlgorithmType == typeof(HMACSHA512)) { return(() => new HMACSHA512()); } else { return(AlgorithmActivator.CreateFactory <KeyedHashAlgorithm>(ValidationAlgorithmType)); } }
private Func <SymmetricAlgorithm> GetSymmetricBlockCipherAlgorithmFactory(ILogger logger) { // basic argument checking if (EncryptionAlgorithmType == null) { throw Error.Common_PropertyCannotBeNullOrEmpty(nameof(EncryptionAlgorithmType)); } typeof(SymmetricAlgorithm).AssertIsAssignableFrom(EncryptionAlgorithmType); if (EncryptionAlgorithmKeySize < 0) { throw Error.Common_PropertyMustBeNonNegative(nameof(EncryptionAlgorithmKeySize)); } if (logger.IsVerboseLevelEnabled()) { logger.LogVerboseF($"Using managed symmetric algorithm '{EncryptionAlgorithmType.FullName}'."); } if (EncryptionAlgorithmType == typeof(Aes)) { Func <Aes> factory = null; #if !DOTNET5_4 if (OSVersionUtil.IsWindows()) { // If we're on desktop CLR and running on Windows, use the FIPS-compliant implementation. factory = () => new AesCryptoServiceProvider(); } #endif return(factory ?? Aes.Create); } else { return(AlgorithmActivator.CreateFactory <SymmetricAlgorithm>(EncryptionAlgorithmType)); } }
public void TestCanActivate_NoParameterlessConstructor() { AlgorithmActivator activator = new AlgorithmActivator(new TestRegistrar()); AlgorithmDefinition d = new AlgorithmDefinition("BadConstructor", null); bool canActivate = activator.CanActivate(d); Assert.IsFalse(canActivate); }
public void TestCanActivate_NotCached() { AlgorithmActivator activator = new AlgorithmActivator(new TestRegistrar()); AlgorithmDefinition d = new AlgorithmDefinition("unknown", new Property[] { }); bool canActivate = activator.CanActivate(d); Assert.IsFalse(canActivate); }
public void TestCanActivate_ValidDefinition() { AlgorithmActivator activator = new AlgorithmActivator(new TestRegistrar()); AlgorithmDefinition d = new AlgorithmDefinition("Test", new Property[] { new Property("Test", typeof(double)) }); bool canActivate = activator.CanActivate(d); Assert.IsTrue(canActivate); }
/// <summary> /// Initializes a new instance of the <see cref="RegistryFactory"/> /// class. /// </summary> /// <param name="algorithmRegistry">The <see cref="IAlgorithmRegistrar"/> /// containing the loaded algorithm information.</param> public RegistryFactory(IAlgorithmRegistrar algorithmRegistry) { if (algorithmRegistry == null) { throw new ArgumentNullException("algorithmRegistry"); } _activator = new AlgorithmActivator(algorithmRegistry); }
private static Func <RSA> GetAsymmetricBlockCipherAlgorithmFactory(RsaEncryptorConfiguration configuration) { // basic argument checking if (configuration.EncryptionAlgorithmType == typeof(RSA)) { return(RSA.Create); } else { return(AlgorithmActivator.CreateFactory <RSA>(configuration.EncryptionAlgorithmType)); } }
public void TestActivate_NoProperties() { AlgorithmActivator activator = new AlgorithmActivator(new TestRegistrar()); AlgorithmDefinition d = new AlgorithmDefinition("Test", new Property[] {}); AlgorithmPlugin p = activator.Activate(d); Assert.IsNotNull(p); Assert.AreEqual(typeof(TestPlugin), p.GetType()); TestPlugin test = p as TestPlugin; Assert.AreEqual(1, test.Test); }
public void TestActivate_Interpreter_NoException() { AlgorithmActivator activator = new AlgorithmActivator(new InterpreterRegistrar()); AlgorithmDefinition d = new AlgorithmDefinition("Test", new Property[] { new Property("Test", typeof(double)) }); AlgorithmPlugin p = activator.Activate(d); Assert.IsNotNull(p); Assert.AreEqual(typeof(TestPluginInterpreter), p.GetType()); TestPluginInterpreter t = p as TestPluginInterpreter; Assert.IsTrue(t.DidInterpret); }
public void TestActivate_OneProperty_UnknownType() { AlgorithmActivator activator = new AlgorithmActivator(new TestRegistrar()); AlgorithmDefinition d = new AlgorithmDefinition("Test", new Property[] { new Property("Test", typeof(string)) }); AlgorithmPlugin p = activator.Activate(d); Assert.IsNotNull(p); Assert.AreEqual(typeof(TestPlugin), p.GetType()); TestPlugin t = p as TestPlugin; Assert.AreEqual(1, t.Test); }
public void TestActivate_OneProperty_KnownType() { double value = 0d; AlgorithmActivator activator = new AlgorithmActivator(new TestRegistrar()); AlgorithmDefinition d = new AlgorithmDefinition("Test", new Property[] { new Property("Test", typeof(double)) { Value = value } }); AlgorithmPlugin p = activator.Activate(d); Assert.IsNotNull(p); Assert.AreEqual(typeof(TestPlugin), p.GetType()); TestPlugin t = p as TestPlugin; Assert.AreEqual(value, t.Test); }
private Func <KeyedHashAlgorithm> GetKeyedHashAlgorithmFactory(ManagedAuthenticatedEncryptorConfiguration configuration) { // basic argument checking if (configuration.ValidationAlgorithmType == null) { throw Error.Common_PropertyCannotBeNullOrEmpty(nameof(configuration.ValidationAlgorithmType)); } _logger.UsingManagedKeyedHashAlgorithm(configuration.ValidationAlgorithmType.FullName !); if (configuration.ValidationAlgorithmType == typeof(HMACSHA256)) { return(() => new HMACSHA256()); } else if (configuration.ValidationAlgorithmType == typeof(HMACSHA512)) { return(() => new HMACSHA512()); } else { return(AlgorithmActivator.CreateFactory <KeyedHashAlgorithm>(configuration.ValidationAlgorithmType)); } }
private Func <KeyedHashAlgorithm> GetKeyedHashAlgorithmFactory(ILogger logger) { // basic argument checking if (ValidationAlgorithmType == null) { throw Error.Common_PropertyCannotBeNullOrEmpty(nameof(ValidationAlgorithmType)); } logger?.UsingManagedKeyedHashAlgorithm(ValidationAlgorithmType.FullName); if (ValidationAlgorithmType == typeof(HMACSHA256)) { return(() => new HMACSHA256()); } else if (ValidationAlgorithmType == typeof(HMACSHA512)) { return(() => new HMACSHA512()); } else { return(AlgorithmActivator.CreateFactory <KeyedHashAlgorithm>(ValidationAlgorithmType)); } }
private Func <SymmetricAlgorithm> GetSymmetricBlockCipherAlgorithmFactory(ManagedAuthenticatedEncryptorConfiguration configuration) { // basic argument checking if (configuration.EncryptionAlgorithmType == null) { throw Error.Common_PropertyCannotBeNullOrEmpty(nameof(configuration.EncryptionAlgorithmType)); } typeof(SymmetricAlgorithm).AssertIsAssignableFrom(configuration.EncryptionAlgorithmType); if (configuration.EncryptionAlgorithmKeySize < 0) { throw Error.Common_PropertyMustBeNonNegative(nameof(configuration.EncryptionAlgorithmKeySize)); } _logger.UsingManagedSymmetricAlgorithm(configuration.EncryptionAlgorithmType.FullName !); if (configuration.EncryptionAlgorithmType == typeof(Aes)) { return(Aes.Create); } else { return(AlgorithmActivator.CreateFactory <SymmetricAlgorithm>(configuration.EncryptionAlgorithmType)); } }
public void TestActivate_NullDefinition() { AlgorithmActivator activator = new AlgorithmActivator(new TestRegistrar()); activator.Activate(null); }
public void TestConstructor_NullRegistrar() { AlgorithmActivator activator = new AlgorithmActivator(null); }
public void TestConstructor_ValidRegistrar() { AlgorithmActivator activator = new AlgorithmActivator(new TestRegistrar()); }