Skip to content

Commit

Permalink
added test for fail case
Browse files Browse the repository at this point in the history
fixed bug with verifying password with hash
  • Loading branch information
PascalBenstrong committed Jan 29, 2022
1 parent 6481f00 commit 3437244
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 6 deletions.
2 changes: 1 addition & 1 deletion FluentPasswordHashing/PasswordHashArguments.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace FluentPasswordHashing
{
internal class PasswordHashArguments
internal record PasswordHashArguments
{
/// <summary>
/// Argon2 hash algorithm to use
Expand Down
7 changes: 3 additions & 4 deletions FluentPasswordHashing/Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,10 @@ private static async ValueTask<byte[]> HashPassword(char[] password, PasswordHas
internal static async ValueTask<PasswordHashArguments> GenerateArgumentsWithHash(PasswordHashArguments arguments, char[] password, int saltLength = 16)
{
var salt = arguments.Salt ?? CreateSalt(saltLength);
var hash = await HashPassword(password,arguments).ConfigureAwait(false);
arguments = arguments with { Salt = salt };
var hash = await HashPassword(password, arguments).ConfigureAwait(false);

arguments.Salt = salt;
arguments.Hash = hash;
return arguments;
return arguments with { Hash = hash };
}

internal static ValueTask<PasswordHashArguments> GenerateArgumentsWithHash(PasswordHashArguments arguments, string password, int saltLength = 16)
Expand Down
18 changes: 17 additions & 1 deletion UnitTest.FluentPasswordHashing/FluentPasswordHashingTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public async Task Generating_A_Hash_Should_Pass()
}

[Theory]
[InlineData("$argon2id$m=16,t=4,p=1$aM8qmYw1xfzvHEYTIg0w1g==$5Hg1fRhHv1uOnOMusawtqg==", "password")]
[InlineData("$argon2id$m=16,t=4,p=1$aM8qmYw1xfzvHEYTIg0w1g==$mQqnrY9rQyMC7SJq039z/A==", "password")]
public async Task Verifying_Password_And_Hash_Should_Pass(string hash, string password)
{
// arrange
Expand All @@ -48,5 +48,21 @@ public async Task Verifying_Password_And_Hash_Should_Pass(string hash, string pa
// assert
isMatch.Should().BeTrue();
}

[Theory]
[InlineData("$argon2id$m=16,t=4,p=1$aM8qmYw1xfzvHEYTIg0w1g==$mQqnrY9rQyMC7SJq039z/A==", "wrong password")]
public async Task Verifying_Password_And_Hash_Should_Fail(string hash, string password)
{
// arrange
var generator = FluentHashing.Create()
.Generator();

// act
var isMatch = await generator.VerifyPassword(hash, password);


// assert
isMatch.Should().BeFalse();
}
}
}

0 comments on commit 3437244

Please sign in to comment.