Example #1
0
        public static string hash_value(string originalText, CryptoHashProviderType providerType)
        {
            HashAlgorithm hashAlgorithm = null;

            switch (providerType)
            {
            case CryptoHashProviderType.Md5:
                hashAlgorithm = new HashAlgorithm(MD5.Create());
                break;

            case CryptoHashProviderType.Sha1:
                hashAlgorithm = new HashAlgorithm(SHA1.Create());
                break;

            case CryptoHashProviderType.Sha256:
                hashAlgorithm = new HashAlgorithm(SHA256.Create());
                break;

            case CryptoHashProviderType.Sha512:
                hashAlgorithm = new HashAlgorithm(SHA512.Create());
                break;
            }

            if (hashAlgorithm == null)
            {
                return(string.Empty);
            }

            var hash = hashAlgorithm.ComputeHash(Encoding.ASCII.GetBytes(originalText));

            return(BitConverter.ToString(hash).Replace("-", string.Empty));
        }
Example #2
0
        private static IHashAlgorithm get_hash_algorithm_static(CryptoHashProviderType algorithmType)
        {
            var fipsOnly = false;

            try
            {
                fipsOnly = CryptoConfig.AllowOnlyFipsAlgorithms;
            }
            catch (Exception ex)
            {
                "chocolatey".Log().Debug("Unable to get FipsPolicy from CryptoConfig:{0} {1}".format_with(Environment.NewLine, ex.Message));
            }

            HashAlgorithm hashAlgorithm = null;

            switch (algorithmType)
            {
            case CryptoHashProviderType.Md5:
                hashAlgorithm = new HashAlgorithm(MD5.Create());
                break;

#if NETFRAMEWORK
            case CryptoHashProviderType.Sha1:
                hashAlgorithm = new HashAlgorithm(fipsOnly ? new SHA1Cng() : SHA1.Create());
                break;

            case CryptoHashProviderType.Sha256:
                hashAlgorithm = new HashAlgorithm(fipsOnly ? new SHA256Cng() : SHA256.Create());
                break;

            case CryptoHashProviderType.Sha512:
                hashAlgorithm = new HashAlgorithm(fipsOnly ? new SHA512Cng() : SHA512.Create());
                break;
#else
            // On .net core there does not exists SHA1Cng/SHA256Cng/SHA512Cng - there exists SHA1Managed alternative
            // but it's not clear whether they map one to one. Maybe some obsolete .NET Framework feature ?
            case CryptoHashProviderType.Sha1:
                hashAlgorithm = new HashAlgorithm(SHA1.Create());
                break;

            case CryptoHashProviderType.Sha256:
                hashAlgorithm = new HashAlgorithm(SHA256.Create());
                break;

            case CryptoHashProviderType.Sha512:
                hashAlgorithm = new HashAlgorithm(SHA512.Create());
                break;
#endif
            }

            return(hashAlgorithm);
        }
Example #3
0
        private static IHashAlgorithm get_hash_algorithm_static(CryptoHashProviderType algorithmType)
        {
            var fipsOnly = false;

            try
            {
                fipsOnly = CryptoConfig.AllowOnlyFipsAlgorithms;
            }
            catch (Exception ex)
            {
                "chocolatey".Log().Debug("Unable to get FipsPolicy from CryptoConfig:{0} {1}".format_with(Environment.NewLine, ex.Message));
            }

            HashAlgorithm hashAlgorithm = null;

            switch (algorithmType)
            {
            case CryptoHashProviderType.Md5:
                hashAlgorithm = new HashAlgorithm(MD5.Create());
                break;

            case CryptoHashProviderType.Sha1:
                hashAlgorithm = new HashAlgorithm(fipsOnly ? new SHA1Cng() : SHA1.Create());
                break;

            case CryptoHashProviderType.Sha256:
                hashAlgorithm = new HashAlgorithm(fipsOnly ? new SHA256Cng() : SHA256.Create());
                break;

            case CryptoHashProviderType.Sha512:
                hashAlgorithm = new HashAlgorithm(fipsOnly ? new SHA512Cng() : SHA512.Create());
                break;
            }

            return(hashAlgorithm);
        }
Example #4
0
        private static IHashAlgorithm get_hash_algorithm_static(CryptoHashProviderType algorithmType)
        {
            var fipsOnly = false;
            try
            {
                fipsOnly = CryptoConfig.AllowOnlyFipsAlgorithms;
            }
            catch (Exception ex)
            {
                "chocolatey".Log().Debug("Unable to get FipsPolicy from CryptoConfig:{0} {1}".format_with(Environment.NewLine, ex.Message));
            }

            HashAlgorithm hashAlgorithm = null;
            switch (algorithmType)
            {
                case CryptoHashProviderType.Md5:
                    hashAlgorithm = new HashAlgorithm(MD5.Create());
                    break;
                case CryptoHashProviderType.Sha1:
                    hashAlgorithm = new HashAlgorithm(fipsOnly ? new SHA1Cng() : SHA1.Create());
                    break;
                case CryptoHashProviderType.Sha256:
                    hashAlgorithm = new HashAlgorithm(fipsOnly ? new SHA256Cng() : SHA256.Create());
                    break;
                case CryptoHashProviderType.Sha512:
                    hashAlgorithm = new HashAlgorithm(fipsOnly ? new SHA512Cng() : SHA512.Create());
                    break;
            }

            return hashAlgorithm;
        }
Example #5
0
        public static string hash_value(string originalText, CryptoHashProviderType providerType)
        {
            HashAlgorithm hashAlgorithm = null;
            switch (providerType)
            {
                case CryptoHashProviderType.Md5:
                    hashAlgorithm = new HashAlgorithm(MD5.Create());
                    break;
                case CryptoHashProviderType.Sha1:
                    hashAlgorithm = new HashAlgorithm(SHA1.Create());
                    break;
                case CryptoHashProviderType.Sha256:
                    hashAlgorithm = new HashAlgorithm(SHA256.Create());
                    break;
                case CryptoHashProviderType.Sha512:
                    hashAlgorithm = new HashAlgorithm(SHA512.Create());
                    break;
            }

            if (hashAlgorithm == null) return string.Empty;

             var hash = hashAlgorithm.ComputeHash(Encoding.ASCII.GetBytes(originalText));
             return BitConverter.ToString(hash).Replace("-", string.Empty);
        }