示例#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));
        }
示例#2
0
        public static string hash_value(string originalText, CryptoHashProviderType providerType)
        {
            IHashAlgorithm hashAlgorithm = get_hash_algorithm_static(providerType);
            if (hashAlgorithm == null) return string.Empty;

             var hash = hashAlgorithm.ComputeHash(Encoding.ASCII.GetBytes(originalText));
             return BitConverter.ToString(hash).Replace("-", string.Empty);
        }
示例#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;

#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);
        }
示例#4
0
        public static string hash_value(string originalText, CryptoHashProviderType providerType)
        {
            IHashAlgorithm hashAlgorithm = get_hash_algorithm_static(providerType);

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

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

            return(BitConverter.ToString(hash).Replace("-", string.Empty));
        }
示例#5
0
        public CrytpoHashProvider(IFileSystem fileSystem, CryptoHashProviderType providerType)
        {
            _fileSystem = fileSystem;

            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;
            }
        }
示例#6
0
        public CrytpoHashProvider(IFileSystem fileSystem, CryptoHashProviderType providerType)
        {
            _fileSystem = fileSystem;

            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;
            }
        }
示例#7
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);
        }
示例#8
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);
        }
示例#9
0
 public void set_hash_algorithm(CryptoHashProviderType algorithmType)
 {
     _hashAlgorithm = get_hash_algorithm_static(algorithmType);
 }
示例#10
0
 public void set_hash_algorithm(CryptoHashProviderType algorithmType)
 {
     _hashAlgorithm = get_hash_algorithm_static(algorithmType);
 }
示例#11
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;
        }