Skip to content

Commit

Permalink
#22 testing benchmark password hashers
Browse files Browse the repository at this point in the history
  • Loading branch information
WingZer0o committed Oct 12, 2024
1 parent bfd44b0 commit 5ceb654
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 0 deletions.
62 changes: 62 additions & 0 deletions cas-dotnet-benchmarks/PasswordHasherBenchmarks.cs
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);
}
}
}
4 changes: 4 additions & 0 deletions cas-dotnet-benchmarks/Program.cs
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>();
26 changes: 26 additions & 0 deletions cas-dotnet-benchmarks/Util.cs
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);
}
}
}
19 changes: 19 additions & 0 deletions cas-dotnet-benchmarks/cas-dotnet-benchmarks.csproj
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>
6 changes: 6 additions & 0 deletions cas-dotnet-sdk.sln
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "cas-dotnet-sdk", "cas-dotne
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "cas-dotnet-sdk-tests", "cas-dotnet-sdk-tests\cas-dotnet-sdk-tests.csproj", "{4904E91B-5A0D-41E9-92CA-0E1BCE987FAE}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "cas-dotnet-benchmarks", "cas-dotnet-benchmarks\cas-dotnet-benchmarks.csproj", "{E6C12EB3-ABBD-4014-8091-BEFAE84D77C5}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -21,6 +23,10 @@ Global
{4904E91B-5A0D-41E9-92CA-0E1BCE987FAE}.Debug|Any CPU.Build.0 = Debug|Any CPU
{4904E91B-5A0D-41E9-92CA-0E1BCE987FAE}.Release|Any CPU.ActiveCfg = Release|Any CPU
{4904E91B-5A0D-41E9-92CA-0E1BCE987FAE}.Release|Any CPU.Build.0 = Release|Any CPU
{E6C12EB3-ABBD-4014-8091-BEFAE84D77C5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{E6C12EB3-ABBD-4014-8091-BEFAE84D77C5}.Debug|Any CPU.Build.0 = Debug|Any CPU
{E6C12EB3-ABBD-4014-8091-BEFAE84D77C5}.Release|Any CPU.ActiveCfg = Release|Any CPU
{E6C12EB3-ABBD-4014-8091-BEFAE84D77C5}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down

0 comments on commit 5ceb654

Please sign in to comment.