Created
September 25, 2019 19:51
-
-
Save Indigo744/b6cce04b8a1bfcef90a43ea1302c4701 to your computer and use it in GitHub Desktop.
C# BCRYPT.Net cost calculator
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
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
Example output:
Note: first value seems to be an outlier...