hash string multiple times in csharp

To hash a string multiple times in C#, you can use a hash function repeatedly on the output of the previous hash operation. Here is an example of hashing a string three times using the SHA256 hash function:

main.cs
using System.Security.Cryptography;
using System.Text;

public static string HashStringMultipleTimes(string input, int iterations)
{
    using (var hasher = SHA256.Create())
    {
        byte[] hashBytes = Encoding.UTF8.GetBytes(input);
        for (int i = 0; i < iterations; i++)
        {
            hashBytes = hasher.ComputeHash(hashBytes);
        }
        return Convert.ToBase64String(hashBytes);
    }
}
416 chars
16 lines

You can call this method with the string you want to hash and the number of iterations:

main.cs
string input = "password123";
int iterations = 3;
string hashedString = HashStringMultipleTimes(input, iterations);
116 chars
4 lines

This will return a hashed string that has been hashed three times using the SHA256 algorithm. Note that the number of iterations should be chosen based on your specific application requirements and the level of security needed.

gistlibby LogSnag