コード例 #1
0
        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));
            }
        }
コード例 #2
0
        public void TestCanActivate_NullDefinition()
        {
            AlgorithmActivator activator = new AlgorithmActivator(new TestRegistrar());
            bool canActivate             = activator.CanActivate(null);

            Assert.IsFalse(canActivate);
        }
コード例 #3
0
 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));
            }
        }
コード例 #6
0
        public void TestCanActivate_NoParameterlessConstructor()
        {
            AlgorithmActivator  activator = new AlgorithmActivator(new TestRegistrar());
            AlgorithmDefinition d         = new AlgorithmDefinition("BadConstructor", null);
            bool canActivate = activator.CanActivate(d);

            Assert.IsFalse(canActivate);
        }
コード例 #7
0
        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);
        }
コード例 #8
0
        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);
        }
コード例 #9
0
ファイル: RegistryFactory.cs プロジェクト: g4idrijs/DIPS
        /// <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);
        }
コード例 #10
0
 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));
     }
 }
コード例 #11
0
        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);
        }
コード例 #12
0
        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);
        }
コード例 #13
0
        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);
        }
コード例 #14
0
        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);
        }
コード例 #15
0
    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));
        }
    }
コード例 #16
0
        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));
            }
        }
コード例 #17
0
    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));
        }
    }
コード例 #18
0
        public void TestActivate_NullDefinition()
        {
            AlgorithmActivator activator = new AlgorithmActivator(new TestRegistrar());

            activator.Activate(null);
        }
コード例 #19
0
 public void TestConstructor_NullRegistrar()
 {
     AlgorithmActivator activator = new AlgorithmActivator(null);
 }
コード例 #20
0
 public void TestConstructor_ValidRegistrar()
 {
     AlgorithmActivator activator = new AlgorithmActivator(new TestRegistrar());
 }