C# BCRYPT.Net cost calculator
namespace BcryptPerformanceTestCSharpDotNet | |
{ | |
using System; | |
using System.Diagnostics; | |
using BCrypt.Net; | |
/// <summary> | |
/// C# BCRYPT.Net cost calculator | |
/// You will need BCrypt.Net-Next Nugget: https://www.nuget.org/packages/BCrypt.Net-Next/ | |
/// </summary> | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
int cost = 3; | |
int first_cost_above_100 = 0; | |
int first_cost_above_500 = 0; | |
long upperTimeLimit = 1000; | |
string password = "this_is_just_a_long_string_to_test_on_U8WNZqmz8ZVBNiNTQR8r"; | |
Console.WriteLine("\nPassword BCRYPT.Net Hash Cost Calculator\n\n"); | |
Console.WriteLine($"We're going to run until the time to generate the hash takes longer than {upperTimeLimit}ms\n"); | |
long time; | |
Stopwatch stopwatch = new Stopwatch(); | |
do | |
{ | |
cost += 1; | |
Console.Write($"\nTesting cost value of {cost}: "); | |
stopwatch.Restart(); | |
BCrypt.HashPassword(password, cost); | |
stopwatch.Stop(); | |
time = stopwatch.ElapsedMilliseconds; | |
Console.Write($"... took {time}ms"); | |
if (first_cost_above_100 == 0 && time > 100) | |
{ | |
first_cost_above_100 = cost; | |
} else if (first_cost_above_500 == 0 && time > 500) | |
{ | |
first_cost_above_500 = cost; | |
} | |
} while (time < upperTimeLimit); | |
Console.WriteLine($"\n\n\nYou should use a cost between {first_cost_above_100} and {first_cost_above_500}"); | |
Console.ReadLine(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This comment has been minimized.
Example output:
Note: first value seems to be an outlier...