Created
December 22, 2023 10:51
-
-
Save flytzen/9b94fc3676b6db4c2d40e20aaaaf1805 to your computer and use it in GitHub Desktop.
Hash iteration tester
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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