Skip to content

Instantly share code, notes, and snippets.

@Indigo744
Created September 25, 2019 19:51
Show Gist options
  • Save Indigo744/b6cce04b8a1bfcef90a43ea1302c4701 to your computer and use it in GitHub Desktop.
Save Indigo744/b6cce04b8a1bfcef90a43ea1302c4701 to your computer and use it in GitHub Desktop.
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();
}
}
}
@Indigo744
Copy link
Author

Example output:

Password BCRYPT.Net Hash Cost Calculator


We're going to run until the time to generate the hash takes longer than 1000ms


Testing cost value of 4: ... took 29ms
Testing cost value of 5: ... took 4ms
Testing cost value of 6: ... took 8ms
Testing cost value of 7: ... took 16ms
Testing cost value of 8: ... took 32ms
Testing cost value of 9: ... took 64ms
Testing cost value of 10: ... took 129ms
Testing cost value of 11: ... took 256ms
Testing cost value of 12: ... took 514ms
Testing cost value of 13: ... took 1033ms


You should use a cost between 10 and 12

Note: first value seems to be an outlier...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment