< Summary

Information
Class: Anichron.API.Security.Argon2PasswordHasher
Assembly: Anichron.API
File(s): /home/runner/work/anichron/anichron/src/Anichron.API/Security/PasswordHasher.cs
Tag: 19_45211875487
Line coverage
100%
Covered lines: 42
Uncovered lines: 0
Coverable lines: 42
Total lines: 95
Line coverage: 100%
Branch coverage
100%
Covered branches: 2
Total branches: 2
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor()100%11100%
.ctor(...)100%11100%
Hash(...)100%11100%
Verify(...)100%22100%
RunArgon2idSecure(...)100%11100%
RunArgon2id(...)100%11100%

File(s)

/home/runner/work/anichron/anichron/src/Anichron.API/Security/PasswordHasher.cs

#LineLine coverage
 1using Anichron.API.Settings;
 2using Konscious.Security.Cryptography;
 3using System.Security.Cryptography;
 4using System.Text;
 5
 6namespace Anichron.API.Security;
 7
 8public interface IPasswordHasher
 9{
 10    string Hash(string password);
 11
 12    // storedHash is null when no such account exists. A null hash still costs a full Argon2
 13    // pass, so verification itself takes the same time either way. That removes the HASHING
 14    // asymmetry only — it does not make a caller's whole code path constant-time.
 15    bool Verify(string password, string? storedHash);
 16}
 17
 18public sealed class Argon2PasswordHasher : IPasswordHasher
 19{
 20    private readonly Func<byte[], byte[], byte[]> argon2;
 21
 22    public Argon2PasswordHasher()
 1223        : this(RunArgon2id)
 1224    {
 1225    }
 26
 2627    internal Argon2PasswordHasher(Func<byte[], byte[], byte[]> argon2) => this.argon2 = argon2;
 28
 29    public string Hash(string password)
 630    {
 631        var salt = RandomNumberGenerator.GetBytes(AppDefaults.Argon2.SaltLength);
 632        var hash = RunArgon2idSecure(password, salt);
 33
 634        var combined = new byte[salt.Length + hash.Length];
 635        salt.CopyTo(combined, 0);
 636        hash.CopyTo(combined, salt.Length);
 637        return Convert.ToBase64String(combined);
 638    }
 39
 40    public bool Verify(string password, string? storedHash)
 1041    {
 42        // ⛔ Equal work, not an early return. Verifying a missing account must cost the same
 43        // Argon2 pass as verifying a real one, so this method leaks nothing about account
 44        // existence. ⚠️ Whether the CALLER leaks it is the caller's problem — AuthService.Login
 45        // still does a database write for a known user that it skips for an unknown one.
 46        //
 47        // The invariant lives here because this class owns the cost. It used to live in an
 48        // AuthService field initializer, which — AuthService being Scoped — burned 64 MiB and
 49        // three Argon2 passes on every request that resolved IAuthService, including ones that
 50        // never read the value. See #173.
 51        //
 52        // ⚠️ `return false` unconditionally, rather than comparing against random bytes:
 53        // correctness must not rest on two random values differing. The FixedTimeEquals of the
 54        // real path is deliberately not mirrored here — it is microseconds against ~100 ms of
 55        // Argon2, so adding it back would buy nothing and reintroduce that dependency.
 1056        if (storedHash is null)
 257        {
 258            var throwawaySalt = RandomNumberGenerator.GetBytes(AppDefaults.Argon2.SaltLength);
 259            CryptographicOperations.ZeroMemory(RunArgon2idSecure(password, throwawaySalt));
 260            return false;
 61        }
 62
 863        var combined = Convert.FromBase64String(storedHash);
 764        var salt = combined[..AppDefaults.Argon2.SaltLength];
 665        var expected = combined[AppDefaults.Argon2.SaltLength..];
 666        var actual = RunArgon2idSecure(password, salt);
 667        return CryptographicOperations.FixedTimeEquals(actual, expected);
 868    }
 69
 70    private byte[] RunArgon2idSecure(string password, byte[] salt)
 1471    {
 1472        var passwordBytes = Encoding.UTF8.GetBytes(password);
 73        try
 1474        {
 1475            return argon2(passwordBytes, salt);
 76        }
 77        // Stryker disable once Block,Statement
 78        finally
 1479        {
 1480            CryptographicOperations.ZeroMemory(passwordBytes);
 1481        }
 1482    }
 83
 84    private static byte[] RunArgon2id(byte[] password, byte[] salt)
 1385    {
 1386        using var argon2 = new Argon2id(password)
 1387        {
 1388            Salt = salt,
 1389            DegreeOfParallelism = AppDefaults.Argon2.Parallelism,
 1390            Iterations = AppDefaults.Argon2.Iterations,
 1391            MemorySize = AppDefaults.Argon2.MemoryKiB,
 1392        };
 1393        return argon2.GetBytes(AppDefaults.Argon2.HashLength);
 1394    }
 95}