コード例 #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));
            }
        }
        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));
            }
        }
コード例 #4
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));
     }
 }
コード例 #5
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));
        }
    }
コード例 #6
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));
            }
        }
コード例 #7
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));
        }
    }