public static byte[] GenerateHash(HashingStrategy strategy, string password, byte[] salt, int iterations = 1000) { var bytes = Encoding.UTF8.GetBytes(password).Concat(salt); switch (strategy) { case HashingStrategy.SHA256: using (var sha256 = new SHA256Managed()) { return sha256.ComputeHash(bytes.ToArray()); } case HashingStrategy.SHA512: using (var sha512 = new SHA512Managed()) { return sha512.ComputeHash(bytes.ToArray()); } case HashingStrategy.PBKDF2: using (var pbkdf2 = new Rfc2898DeriveBytes(password, salt) { IterationCount = iterations }) { return pbkdf2.GetBytes(salt.Length); } } throw new NotImplementedException(); }
/// <summary> /// Builds a hash set around a strategy for hashing and returns it to the user. /// </summary> /// <param name="the_strategy"></param> /// <returns></returns> public static HashSet <T> getHashSet(HashingStrategy the_strategy) { //go through the strategies and build a hash set accordingly switch (the_strategy) { case HashingStrategy.ModTableSize: return(new StrategyHashSet <T>(modTableSize, "Math.abs( item.GetHashCode() ) % my_table.length;")); case HashingStrategy.MultiplyPrimeModPrimeModTable: return(new StrategyHashSet <T>(multiplyPrimeModPrime, "int hash = item.GetHashCode();" + " Math.abs( 37 * hash + hash % 37 ) % my_table.length;")); default: throw new ArgumentException("No such enum value found."); } }
public static byte[] GenerateSalt(HashingStrategy strategy) { int length = 0; switch (strategy) { case HashingStrategy.SHA256: length = 32; break; case HashingStrategy.SHA512: length = 64; break; case HashingStrategy.PBKDF2: length = 64; break; } using (var random = new RNGCryptoServiceProvider()) { var bytes = new byte[length]; random.GetBytes(bytes); return bytes; } }