public void UniqueSaltedHashes()
        {
            IHashProvider hashProviderWithSalt = SaltedHashProvider;

            byte[] providerHash1 = hashProviderWithSalt.CreateHash(plainText);
            byte[] providerHash2 = hashProviderWithSalt.CreateHash(plainText);
            Assert.IsFalse(CryptographyUtility.CompareBytes(providerHash1, providerHash2), "compare");
        }
Example #2
0
        public async Task <User> AddUserAsync(ShortUserModel userModel)
        {
            if (await AnyAsync(a => a.Login == userModel.Login))
            {
                throw new Exception("User exist");
            }

            userModel.Password = _hashProvider.CreateHash(userModel.Password);

            var user = _mapper.Map <User>(userModel);

            return(await AddAsync(user));
        }
Example #3
0
 internal static string CreateHash(IHashProvider provider, string plaintext)
 {
     byte[] bytes   = Encoding.Unicode.GetBytes(plaintext);
     byte[] inArray = provider.CreateHash(bytes);
     CryptographyUtility.GetRandomBytes(bytes);
     return(Convert.ToBase64String(inArray));
 }
Example #4
0
 internal static string CreateHash(IHashProvider provider, string plaintext)
 {
     byte[] plainTextBytes = UnicodeEncoding.Unicode.GetBytes(plaintext);
     byte[] resultBytes    = provider.CreateHash(plainTextBytes);
     CryptographyUtility.GetRandomBytes(plainTextBytes);
     return(Convert.ToBase64String(resultBytes));
 }
Example #5
0
        private void btnSave_Click(object sender, EventArgs e)
        {
            if (tbxPassword1.Text != tbxPassword2.Text)
            {
                MessageBox.Show(SR.PasswordsMustMatch);
                return;
            }

            byte[] password = hashProvider.CreateHash(Encoding.Unicode.GetBytes(tbxPassword1.Text));

            if (editMode)
            {
                userRoleMgr.ChangeUserPassword(tbxUser.Text, password);
            }
            else if (userRoleMgr.UserExists(tbxUser.Text))
            {
                MessageBox.Show(SR.UserAlreadyExists);
                return;
            }
            else
            {
                userRoleMgr.CreateUser(tbxUser.Text, password);
            }

            DialogResult = DialogResult.OK;
        }
        public void HashSha1()
        {
            IHashProvider hashProvider = HashProviderHelper.DefaultHashProvider;
            SHA1          sha1         = SHA1Managed.Create();

            byte[] origHash     = sha1.ComputeHash(plainText);
            byte[] providerHash = hashProvider.CreateHash(plainText);

            Assert.IsTrue(CryptographyUtility.CompareBytes(origHash, providerHash));
        }
Example #7
0
        internal static byte[] CreateHash(string hashInstance, byte[] plaintext, ConfigurationContext context)
        {
            ArgumentValidation.CheckForNullReference(hashInstance, "hashInstance");
            ArgumentValidation.CheckForEmptyString(hashInstance, "hashInstance");

            HashProviderFactory factory      = new HashProviderFactory(context);
            IHashProvider       hashProvider = factory.CreateHashProvider(hashInstance);

            return(hashProvider.CreateHash(plaintext));
        }
        public void CompareHashWithSalt()
        {
            IHashProvider hashProvider = SaltedHashProvider;

            byte[] providerHash = hashProvider.CreateHash(plainText);
            Assert.IsTrue(hashProvider.CompareHash(plainText, providerHash), "true");

            byte[] badHash = new byte[50];
            RNGCryptoServiceProvider.Create().GetBytes(badHash);
            Assert.IsFalse(hashProvider.CompareHash(plainText, badHash), "false");
        }
        public void HashWithSalt()
        {
            IHashProvider hashProviderWithSalt = SaltedHashProvider;
            IHashProvider hashProvider         = DefaultHashProvider;

            byte[] origHash1     = hashProvider.CreateHash(plainText);
            byte[] providerHash1 = hashProviderWithSalt.CreateHash(plainText);

            Assert.IsFalse(CryptographyUtility.CompareBytes(origHash1, providerHash1), "original");
            Assert.IsFalse(CryptographyUtility.CompareBytes(plainText, providerHash1), "plain");
        }
        /// <summary>
        /// Hash all entities based off <see cref="HashAttribute"/> on property
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="entities">Entities to hash</param>
        /// <returns>Hashed entities</returns>
        public IEnumerable <T> Hash <T>(IEnumerable <T> entities) where T : IEntity
        {
            if (!_hashEnabled)
            {
                return(entities);
            }

            var type           = typeof(T);
            var hashProperties = GetPropertiesToHash(type);

            if (hashProperties.IsNullOrEmpty())
            {
                return(entities);
            }

            var hashPropertyKey = GetHashKeyProperty(type);
            var hashedEntities  = entities.AsParallel().Select(e =>
            {
                var hashKey = type.GetProperty(hashPropertyKey).GetValue(e).ToString();
                foreach (var propertyName in hashProperties)
                {
                    // Read the unhashed property
                    var unhashed = type.GetProperty(propertyName).GetValue(e).ToString();

                    var existingSalt = _saltCache.Get(hashKey);
                    var hash         = _hashAlgorithm.CreateHash(unhashed, existingSalt);

                    // Set the hashed value onto the property and add the salt too
                    type.GetProperty(propertyName).SetValue(e, hash.Hash);
                    type.GetProperty("Salt").SetValue(e, hash.Salt);
                }

                return(e);
            });

            // Add salt to cache
            foreach (var entity in hashedEntities)
            {
                var salt = type.GetProperty("Salt").GetValue(entity);
                if (salt != null)
                {
                    _saltCache.TryAdd(entity.Word, salt.ToString());
                }
            }

            return(entities);
        }
Example #11
0
        /// <overrides>
        /// Computes the hash value of plain text using the given hash provider instance
        /// </overrides>
        /// <summary>
        /// Computes the hash value of plain text using the given hash provider instance
        /// </summary>
        /// <param name="hashInstance">A hash instance from configuration.</param>
        /// <param name="plaintext">The input for which to compute the hash.</param>
        /// <returns>The computed hash code.</returns>
        public static byte[] CreateHash(string hashInstance, byte[] plaintext)
        {
            if (string.IsNullOrEmpty(hashInstance))
            {
                throw new ArgumentException(Resources.ExceptionNullOrEmptyString, "hashInstance");
            }

            try
            {
                HashProviderFactory factory      = new HashProviderFactory(ConfigurationSourceFactory.Create());
                IHashProvider       hashProvider = factory.Create(hashInstance);

                return(hashProvider.CreateHash(plaintext));
            }
            catch (ConfigurationErrorsException configurationException)
            {
                TryLogHashConfigurationError(configurationException, hashInstance);

                throw;
            }
        }
Example #12
0
 public void ThrowExceptionWhenByteArrayIsNull()
 {
     defaultHashProvider.CreateHash(null);
 }
        /// <overrides>
        /// Computes the hash value of plain text using the given hash provider instance
        /// </overrides>
        /// <summary>
        /// Computes the hash value of plain text using the given hash provider instance
        /// </summary>
        /// <param name="hashInstance">A hash instance from configuration.</param>
        /// <param name="plaintext">The input for which to compute the hash.</param>
        /// <returns>The computed hash code.</returns>
        public override byte[] CreateHash(string hashInstance, byte[] plaintext)
        {
            IHashProvider hashProvider = GetHashProvider(hashInstance);

            return(hashProvider.CreateHash(plaintext));
        }
 /// <summary> Creates the hash code for specified object. </summary>
 /// <param name="obj">The obj.</param>
 /// <returns></returns>
 public string CreateHash(object obj)
 {
     return((obj != null) ? HashProvider.CreateHash(obj) : null);
 }
 internal static string CreateHash(IHashProvider provider, string plaintext)
 {
     byte[] plainTextBytes = UnicodeEncoding.Unicode.GetBytes(plaintext);
     byte[] resultBytes = provider.CreateHash(plainTextBytes);
     CryptographyUtility.GetRandomBytes(plainTextBytes);
     return Convert.ToBase64String(resultBytes);
 }
Example #16
0
 internal static string CreateHash(IHashProvider provider, string plaintext)
 {
     byte[] bytes = Encoding.Unicode.GetBytes(plaintext);
     byte[] inArray = provider.CreateHash(bytes);
     CryptographyUtility.GetRandomBytes(bytes);
     return Convert.ToBase64String(inArray);
 }