42 lines
1.1 KiB
C#
42 lines
1.1 KiB
C#
using System;
|
|
using StellaOps.Cryptography;
|
|
using Xunit;
|
|
|
|
namespace StellaOps.Cryptography.Tests;
|
|
|
|
public class Argon2idPasswordHasherTests
|
|
{
|
|
private readonly Argon2idPasswordHasher hasher = new();
|
|
|
|
[Fact]
|
|
public void Hash_ProducesPhcEncodedString()
|
|
{
|
|
var options = new PasswordHashOptions();
|
|
var encoded = hasher.Hash("s3cret", options);
|
|
|
|
Assert.StartsWith("$argon2id$", encoded, StringComparison.Ordinal);
|
|
}
|
|
|
|
[Fact]
|
|
public void Verify_ReturnsTrue_ForCorrectPassword()
|
|
{
|
|
var options = new PasswordHashOptions();
|
|
var encoded = hasher.Hash("s3cret", options);
|
|
|
|
Assert.True(hasher.Verify("s3cret", encoded));
|
|
Assert.False(hasher.Verify("wrong", encoded));
|
|
}
|
|
|
|
[Fact]
|
|
public void NeedsRehash_ReturnsTrue_WhenParametersChange()
|
|
{
|
|
var options = new PasswordHashOptions();
|
|
var encoded = hasher.Hash("s3cret", options);
|
|
|
|
var updated = options with { Iterations = options.Iterations + 1 };
|
|
|
|
Assert.True(hasher.NeedsRehash(encoded, updated));
|
|
Assert.False(hasher.NeedsRehash(encoded, options));
|
|
}
|
|
}
|