-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#22 testing benchmark password hashers
- Loading branch information
Showing
5 changed files
with
117 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
using BenchmarkDotNet.Attributes; | ||
using CasDotnetSdk.PasswordHashers; | ||
|
||
namespace cas_dotnet_benchmarks | ||
{ | ||
public class PasswordHasherBenchmarks | ||
{ | ||
private string _password { get; set; } | ||
private string _argon2Hash { get; set; } | ||
private string _bcryptHash { get; set; } | ||
private string _scryptHash { get; set; } | ||
private Argon2Wrapper _argon2 { get; set; } | ||
private BcryptWrapper _bcrypt { get; set; } | ||
private SCryptWrapper _scrypt { get; set; } | ||
public PasswordHasherBenchmarks() | ||
{ | ||
this._password = Util.GeneratePassword(15); | ||
this._argon2 = new Argon2Wrapper(); | ||
this._scrypt = new SCryptWrapper(); | ||
this._bcrypt = new BcryptWrapper(); | ||
this._argon2Hash = this._argon2.HashPassword(this._password); | ||
this._bcryptHash = this._bcrypt.HashPassword(this._password); | ||
this._scryptHash = this._scrypt.HashPassword(this._password); | ||
} | ||
|
||
[Benchmark] | ||
public string Argon2Hash() | ||
{ | ||
return this._argon2.HashPassword(this._password); | ||
} | ||
|
||
[Benchmark] | ||
public bool Argon2Verify() | ||
{ | ||
return this._argon2.Verify(this._argon2Hash, this._password); | ||
} | ||
|
||
[Benchmark] | ||
public string BCryptHash() | ||
{ | ||
return this._bcrypt.HashPassword(this._password); | ||
} | ||
|
||
[Benchmark] | ||
public bool BCryptVerify() | ||
{ | ||
return this._bcrypt.Verify(this._bcryptHash, this._password); | ||
} | ||
|
||
[Benchmark] | ||
public string SCryptHash() | ||
{ | ||
return this._bcrypt.HashPassword(this._password); | ||
} | ||
|
||
[Benchmark] | ||
public bool SCryptVerify() | ||
{ | ||
return this._scrypt.Verify(this._scryptHash, this._password); | ||
} | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
using BenchmarkDotNet.Running; | ||
using cas_dotnet_benchmarks; | ||
|
||
var summary = BenchmarkRunner.Run<PasswordHasherBenchmarks>(); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
using System.Security.Cryptography; | ||
|
||
namespace cas_dotnet_benchmarks | ||
{ | ||
public static class Util | ||
{ | ||
public static string GeneratePassword(int length) | ||
{ | ||
const string validChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890!@#$%^&*()"; | ||
char[] passwordChars = new char[length]; | ||
|
||
using (var rng = RandomNumberGenerator.Create()) | ||
{ | ||
byte[] randomBytes = new byte[length]; | ||
rng.GetBytes(randomBytes); | ||
|
||
for (int i = 0; i < length; i++) | ||
{ | ||
passwordChars[i] = validChars[randomBytes[i] % validChars.Length]; | ||
} | ||
} | ||
|
||
return new string(passwordChars); | ||
} | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>net8.0</TargetFramework> | ||
<RootNamespace>cas_dotnet_benchmarks</RootNamespace> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="BenchmarkDotNet" Version="0.14.0" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\cas-dotnet-sdk\cas-dotnet-sdk.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
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