Tag Archives: hash
How to hash a string with salt using a multi-algorithm method
It is considered a very bad idea to store user credentials in plain text, especially passwords. For that very reason it is always a good idea to hash passwords before you store them, ideally with a unique salt that you can store in another location. That way if for whatever reason your database is compromised your users passwords will not be in plain text and it will require a bit of work in order to find out what each password is, especially if the salt for each password is stored somewhere else.
The following method is a multi algorithm method, that means that with this single method you can use more than one algorithm to hash your data. The snippet below shows an example how to hash your data with a single method using seven different algorithms.
Our main method that will do all the work for us:
private string StringToHash(string data, string salt, HashAlgorithm algorithm)
byte saltedBytes = Encoding.UTF8.GetBytes(data + salt); // Combine the data with the salt
byte hashedBytes = algorithm.ComputeHash(saltedBytes); // Compute the hash value of our input