Skip to content

Instantly share code, notes, and snippets.

@flytzen
Created December 22, 2023 10:51
Show Gist options
  • Save flytzen/9b94fc3676b6db4c2d40e20aaaaf1805 to your computer and use it in GitHub Desktop.
Save flytzen/9b94fc3676b6db4c2d40e20aaaaf1805 to your computer and use it in GitHub Desktop.
Hash iteration tester
// First:
// dotnet add package BenchmarkDotNet
// Run like this:
// dotnet run -c Release
using System.Security.Cryptography;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Running;
BenchmarkRunner.Run<TestHashIterations>();
public class TestHashIterations
{
private byte[] byteArray;
[Params(3, 4, 5, 6, 7)]
public int IterationExponent { get; set; }
[GlobalSetup]
public void Setup()
{
byteArray = new byte[32]; // I am assuming a 32-character API key - you will want to change this to match your key length
var random = new Random();
random.NextBytes(byteArray);
}
[Benchmark]
public byte[] Hash() => Hasher.HashByteArrayWithIterations(byteArray, (int)Math.Pow(10, IterationExponent));
}
public static class Hasher
{
public static byte[] HashByteArrayWithIterations(byte[] byteArray, int totalIterationCount)
{
using (var sha256 = SHA256.Create())
{
for (int iterationCount = 0; iterationCount < totalIterationCount; iterationCount++)
{
byteArray = sha256.ComputeHash(byteArray);
}
return byteArray;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment