public static bool CheckSaltedHash(string stringToCheck, Salted_Hash saltedHash) { if (stringToCheck == null) { throw new ArgumentNullException("password"); } else if (saltedHash.Hash == null) { throw new ArgumentNullException("salt"); } else if (saltedHash.Salt == null) { throw new ArgumentNullException("hashedPassword"); } else if (saltedHash.Iterations <= 0) { throw new ArgumentOutOfRangeException("iterations", "number must be positive"); } byte[] hashGenerated = null; using (Rfc2898DeriveBytes hashDriver = new Rfc2898DeriveBytes( stringToCheck, // string to hash saltedHash.Salt, // salt saltedHash.Iterations)) // hash iterations { hashGenerated = hashDriver.GetBytes(hash_lenght); } return(ByteArraysEqual(hashGenerated, saltedHash.Hash)); }
public static Salted_Hash CreateSaltedHash(string stringToHash, int iterations, int saltLenght = 64) { if (stringToHash == null) { throw new ArgumentNullException("password"); } else if (iterations <= 0) { throw new ArgumentOutOfRangeException("iterations", "number must be positive"); } else if (saltLenght <= 0) { throw new ArgumentOutOfRangeException("saltLenght", "number must be positive"); } Salted_Hash salted_Hash = new Salted_Hash() { Salt = Security.CreateToken(saltLenght), Iterations = iterations }; using (Rfc2898DeriveBytes hashDriver = new Rfc2898DeriveBytes( stringToHash, // string to hash salted_Hash.Salt, // salt iterations)) // hash iterations { salted_Hash.Hash = hashDriver.GetBytes(hash_lenght); } return(salted_Hash); }