Пример #1
0
        internal static HashAlgorithm CreateHashAlgorithm(string digestMethod)
        {
            object algorithmObject = CryptoAlgorithms.GetAlgorithmFromConfig(digestMethod);

            if (algorithmObject != null)
            {
                HashAlgorithm hashAlgorithm = algorithmObject as HashAlgorithm;
                if (hashAlgorithm != null)
                {
                    return(hashAlgorithm);
                }
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperWarning(new MessageSecurityException(SR.Format(SR.CustomCryptoAlgorithmIsNotValidHashAlgorithm, digestMethod)));
            }

            switch (digestMethod)
            {
            case SecurityAlgorithms.Sha1Digest:
                return(SHA1.Create());

            case SecurityAlgorithms.Sha256Digest:
                return(SHA256.Create());

            default:
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperWarning(new MessageSecurityException(SR.Format(SR.UnsupportedCryptoAlgorithm, digestMethod)));
            }
        }
Пример #2
0
        internal static HashAlgorithm CreateHashForAsymmetricSignature(string signatureMethod)
        {
            object algorithmObject = CryptoAlgorithms.GetAlgorithmFromConfig(signatureMethod);

            if (algorithmObject != null)
            {
                HashAlgorithm        hashAlgorithm;
                SignatureDescription signatureDescription = algorithmObject as SignatureDescription;

                if (signatureDescription != null)
                {
                    hashAlgorithm = signatureDescription.CreateDigest();
                    if (hashAlgorithm != null)
                    {
                        return(hashAlgorithm);
                    }
                }

                hashAlgorithm = algorithmObject as HashAlgorithm;
                if (hashAlgorithm != null)
                {
                    return(hashAlgorithm);
                }

                throw DiagnosticUtility.ExceptionUtility.ThrowHelperWarning(new MessageSecurityException(SR.Format(SR.CustomCryptoAlgorithmIsNotValidAsymmetricSignature, signatureMethod)));
            }

            switch (signatureMethod)
            {
            case SecurityAlgorithms.RsaSha1Signature:
            case SecurityAlgorithms.DsaSha1Signature:
                return(SHA1.Create());

            case SecurityAlgorithms.RsaSha256Signature:
                return(SHA256.Create());

            default:
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperWarning(new MessageSecurityException(SR.Format(SR.UnsupportedCryptoAlgorithm, signatureMethod)));
            }
        }
Пример #3
0
        internal static bool IsSymmetricSupportedAlgorithm(string algorithm, int keySize)
        {
            bool   found           = false;
            object algorithmObject = null;

            try
            {
                algorithmObject = CryptoAlgorithms.GetAlgorithmFromConfig(algorithm);
            }
            catch (InvalidOperationException)
            {
                algorithmObject = null;
                // We swallow the exception and continue.
            }
            if (algorithmObject != null)
            {
                SymmetricAlgorithm symmetricAlgorithm = algorithmObject as SymmetricAlgorithm;
                KeyedHashAlgorithm keyedHashAlgorithm = algorithmObject as KeyedHashAlgorithm;
                if (symmetricAlgorithm != null || keyedHashAlgorithm != null)
                {
                    found = true;
                }
            }

            switch (algorithm)
            {
            case SecurityAlgorithms.DsaSha1Signature:
            case SecurityAlgorithms.RsaSha1Signature:
            case SecurityAlgorithms.RsaSha256Signature:
            case SecurityAlgorithms.RsaOaepKeyWrap:
            case SecurityAlgorithms.RsaV15KeyWrap:
                return(false);

            case SecurityAlgorithms.HmacSha1Signature:
            case SecurityAlgorithms.HmacSha256Signature:
            case SecurityAlgorithms.Psha1KeyDerivation:
            case SecurityAlgorithms.Psha1KeyDerivationDec2005:
                return(true);

            case SecurityAlgorithms.Aes128Encryption:
            case SecurityAlgorithms.Aes128KeyWrap:
                return(keySize == 128);

            case SecurityAlgorithms.Aes192Encryption:
            case SecurityAlgorithms.Aes192KeyWrap:
                return(keySize == 192);

            case SecurityAlgorithms.Aes256Encryption:
            case SecurityAlgorithms.Aes256KeyWrap:
                return(keySize == 256);

            case SecurityAlgorithms.TripleDesEncryption:
            case SecurityAlgorithms.TripleDesKeyWrap:
                return(keySize == 128 || keySize == 192);

            default:
                if (found)
                {
                    return(true);
                }
                return(false);
            }
        }
Пример #4
0
        static CryptoAlgorithmType GetAlgorithmType(string algorithm)
        {
            object algorithmObject = null;

            try
            {
                algorithmObject = CryptoAlgorithms.GetAlgorithmFromConfig(algorithm);
            }
            catch (InvalidOperationException)
            {
                algorithmObject = null;
                // We swallow the exception and continue.
            }
            if (algorithmObject != null)
            {
                SymmetricAlgorithm symmetricAlgorithm = algorithmObject as SymmetricAlgorithm;
                KeyedHashAlgorithm keyedHashAlgorithm = algorithmObject as KeyedHashAlgorithm;
                if (symmetricAlgorithm != null || keyedHashAlgorithm != null)
                {
                    return(CryptoAlgorithmType.Symmetric);
                }

                // NOTE: A KeyedHashAlgorithm is symmetric in nature.

                AsymmetricAlgorithm  asymmetricAlgorithm  = algorithmObject as AsymmetricAlgorithm;
                SignatureDescription signatureDescription = algorithmObject as SignatureDescription;
                if (asymmetricAlgorithm != null || signatureDescription != null)
                {
                    return(CryptoAlgorithmType.Asymmetric);
                }

                return(CryptoAlgorithmType.Unknown);
            }

            switch (algorithm)
            {
            case SecurityAlgorithms.DsaSha1Signature:
            case SecurityAlgorithms.RsaSha1Signature:
            case SecurityAlgorithms.RsaSha256Signature:
            case SecurityAlgorithms.RsaOaepKeyWrap:
            case SecurityAlgorithms.RsaV15KeyWrap:
                return(CryptoAlgorithmType.Asymmetric);

            case SecurityAlgorithms.HmacSha1Signature:
            case SecurityAlgorithms.HmacSha256Signature:
            case SecurityAlgorithms.Aes128Encryption:
            case SecurityAlgorithms.Aes192Encryption:
            case SecurityAlgorithms.Aes256Encryption:
            case SecurityAlgorithms.TripleDesEncryption:
            case SecurityAlgorithms.Aes128KeyWrap:
            case SecurityAlgorithms.Aes192KeyWrap:
            case SecurityAlgorithms.Aes256KeyWrap:
            case SecurityAlgorithms.TripleDesKeyWrap:
            case SecurityAlgorithms.Psha1KeyDerivation:
            case SecurityAlgorithms.Psha1KeyDerivationDec2005:
                return(CryptoAlgorithmType.Symmetric);

            default:
                return(CryptoAlgorithmType.Unknown);
            }
        }