Skip to content

Commit

Permalink
error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
Haik committed Oct 31, 2023
1 parent eb082a6 commit dda55f4
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 10 deletions.
13 changes: 7 additions & 6 deletions Pandatech.Crypto.Tests/Argon2IdTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ public class Argon2IdTests
[Fact]
public void HashVerify_ShouldFailForDifferentArgonConfigs()
{
var argon2Id = new Argon2Id(new Argon2IdOptions());
var argon2Id = new Argon2Id();
var argon2Id2 = new Argon2Id(new Argon2IdOptions { SaltSize = 16, MemorySize = 128, DegreeOfParallelism = 1, Iterations = 1 });
var password = RandomPassword.Generate(32, true, true, true, true);
var hash = argon2Id.HashPassword(password);
Expand All @@ -15,7 +15,7 @@ public void HashVerify_ShouldFailForDifferentArgonConfigs()
[Fact]
public void HashVerify_ShouldBeValid()
{
var argon2Id = new Argon2Id(new Argon2IdOptions());
var argon2Id = new Argon2Id();

var password = RandomPassword.Generate(32, true, true, true, true);
var hash = argon2Id.HashPassword(password);
Expand All @@ -26,15 +26,16 @@ public void HashVerify_ShouldBeValid()
[Fact]
public void HashVerify_InvalidPassword_ShouldBeInvalid()
{
var argon2Id = new Argon2Id(new Argon2IdOptions());
var argon2Id = new Argon2Id();
var password = RandomPassword.Generate(32, true, true, true, true);
var hash = argon2Id.HashPassword(password);
Assert.False(argon2Id.VerifyHash("SomePassword", hash));
}

[Fact]
public void DifferentPasswords_ShouldHaveDifferentHashes()
{
var argon2Id = new Argon2Id(new Argon2IdOptions());
var argon2Id = new Argon2Id();
var password1 = RandomPassword.Generate(32, true, true, true, true);
var password2 = RandomPassword.Generate(32, true, true, true, true);
var hash1 = argon2Id.HashPassword(password1);
Expand All @@ -46,14 +47,14 @@ public void DifferentPasswords_ShouldHaveDifferentHashes()
[Fact]
public void HashPassword_EmptyPassword_ShouldThrowException()
{
var argon2Id = new Argon2Id(new Argon2IdOptions());
var argon2Id = new Argon2Id();
Assert.Throws<ArgumentException>(() => argon2Id.HashPassword(""));
}

[Fact]
public void VerifyHash_NullHash_ShouldThrowException()
{
var argon2Id = new Argon2Id(new Argon2IdOptions());
var argon2Id = new Argon2Id();
Assert.Throws<ArgumentException>(() => argon2Id.VerifyHash("password", null!));
}
}
5 changes: 3 additions & 2 deletions Pandatech.Crypto/Argon2Id.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ namespace Pandatech.Crypto;
public class Argon2Id
{
private readonly Argon2IdOptions _options;

public Argon2Id(Argon2IdOptions options)
{
_options = options;
}

public Argon2Id()
{
_options = new Argon2IdOptions();
Expand All @@ -25,6 +25,7 @@ public byte[] HashPassword(string password)
{
throw new ArgumentException("Password cannot be null or empty.", nameof(password));
}

var salt = Random.GenerateBytes(_options.SaltSize);
return HashPassword(password, salt);
}
Expand All @@ -50,7 +51,7 @@ public bool VerifyHash(string password, byte[] hash)
{
throw new ArgumentException($"Hash must be at least {SaltSize} bytes.", nameof(hash));
}

var salt = hash.Take(_options.SaltSize).ToArray();

var newHash = HashPassword(password, salt);
Expand Down
4 changes: 2 additions & 2 deletions Pandatech.Crypto/Pandatech.Crypto.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<Version>2.1.0</Version>
<Version>2.1.1</Version>
<Title>Pandatech.Crypto</Title>
<Authors>Pandatech</Authors>
<PackageIcon>pandatech.png</PackageIcon>
Expand All @@ -26,7 +26,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="BouncyCastle.NetCore" Version="2.2.2" />
<PackageReference Include="BouncyCastle.NetCore" Version="2.2.1" />
<PackageReference Include="Konscious.Security.Cryptography.Argon2" Version="1.3.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="7.0.0" />
</ItemGroup>
Expand Down

0 comments on commit dda55f4

Please sign in to comment.