Exemplo n.º 1
0
        private Boolean CompareHash(Type hashAlgorithmType, Byte[] plainText, Byte[] hashedBytes, Int32 saltLength = 16)
        {
            var hashAlgorithm = Activator.CreateInstance(hashAlgorithmType, true) as HashAlgorithm;

            if (hashAlgorithm == null)
            {
                throw new InvalidOperationException(String.Format("Could not create instance of type {0}.", hashAlgorithmType));
            }

            var salt       = CryptographyUtility.GetBytes(hashedBytes, saltLength);
            var targetHash = CryptographyUtility.CombineBytes(salt, hashAlgorithm.ComputeHash(CryptographyUtility.CombineBytes(salt, plainText)));

            return(CryptographyUtility.CompareBytes(hashedBytes, targetHash));
        }
Exemplo n.º 2
0
        private Boolean CompareHash(Type keyedHashAlgorithmType, Byte[] symmetricKey, Byte[] plainText, Byte[] hashedText, Boolean saltEnabled = true, DataProtectionScope dataProtectionScope = DataProtectionScope.LocalMachine)
        {
            if (symmetricKey == null || plainText == null || hashedText == null)
            {
                return(false);
            }

            var keyedHashAlgorithm = Activator.CreateInstance(keyedHashAlgorithmType, true) as KeyedHashAlgorithm;

            if (keyedHashAlgorithm == null)
            {
                throw new InvalidOperationException(String.Format("Could not create instance of type {0}.", keyedHashAlgorithmType));
            }

            keyedHashAlgorithm.Key = symmetricKey;
            var hashedPlainText = keyedHashAlgorithm.ComputeHash(plainText);

            // TODO: (DG) Support SALT!
            return(CryptographyUtility.CompareBytes(hashedPlainText, hashedText));
        }
Exemplo n.º 3
0
        private Byte[] CreateHash(Type hashAlgorithmType, Byte[] plainText, Int32 saltLength = 16)
        {
            var hashAlgorithm = Activator.CreateInstance(hashAlgorithmType, true) as HashAlgorithm;

            if (hashAlgorithm == null)
            {
                throw new InvalidOperationException(String.Format("Could not create instance of type {0}.", hashAlgorithmType));
            }

            // Generate salt
            var salt = new Byte[saltLength];

            _RngCryptoServiceProvider.GetNonZeroBytes(salt);

            // Compute hash
            var hashedText = hashAlgorithm.ComputeHash(CryptographyUtility.CombineBytes(salt, plainText));

            // Randomize plain text
            _RngCryptoServiceProvider.GetBytes(plainText);

            return(CryptographyUtility.CombineBytes(salt, hashedText));
        }