< Summary - Combined Code Coverage

Information
Class: NLightning.Infrastructure.Crypto.Hashes.Sha256
Assembly: NLightning.Infrastructure
File(s): /home/runner/work/nlightning/nlightning/src/NLightning.Infrastructure/Crypto/Hashes/Sha256.cs
Tag: 36_15743069263
Line coverage
100%
Covered lines: 25
Uncovered lines: 0
Coverable lines: 25
Total lines: 81
Line coverage: 100%
Branch coverage
100%
Covered branches: 4
Total branches: 4
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%
AppendData(...)100%22100%
GetHashAndReset(...)100%11100%
Reset()100%11100%
ReleaseUnmanagedResources()100%11100%
Dispose(...)100%22100%
Dispose()100%11100%
Finalize()100%11100%

File(s)

/home/runner/work/nlightning/nlightning/src/NLightning.Infrastructure/Crypto/Hashes/Sha256.cs

#LineLine coverage
 1using System.Diagnostics;
 2
 3namespace NLightning.Infrastructure.Crypto.Hashes;
 4
 5using Domain.Crypto.Constants;
 6using Domain.Crypto.Hashes;
 7using Factories;
 8using Interfaces;
 9
 10/// <summary>
 11/// SHA-256 from <see href="https://nvlpubs.nist.gov/nistpubs/FIPS/NIST.FIPS.180-4.pdf">FIPS 180-4</see>.
 12/// </summary>
 13public sealed class Sha256 : ISha256
 14{
 15    private readonly ICryptoProvider _cryptoProvider;
 16    private readonly IntPtr _state;
 17
 579618    public Sha256()
 19    {
 579620        _cryptoProvider = CryptoFactory.GetCryptoProvider();
 579621        _state = _cryptoProvider.MemoryAlloc(CryptoConstants.LibsodiumSha256StateLen);
 579622        Reset();
 579623    }
 24
 25    /// <summary>
 26    /// Appends the specified data to the data already processed in the hash.
 27    /// </summary>
 28    public void AppendData(ReadOnlySpan<byte> data)
 29    {
 8431230        if (!data.IsEmpty)
 31        {
 8380032            _cryptoProvider.Sha256Update(_state, data);
 33        }
 8431234    }
 35
 36    /// <summary>
 37    /// Retrieves the hash for the accumulated data into the hash parameter,
 38    /// and resets the object to its initial state.
 39    /// </summary>
 40    public void GetHashAndReset(Span<byte> hash)
 41    {
 42        Debug.Assert(hash.Length == CryptoConstants.Sha256HashLen);
 43
 8139644        _cryptoProvider.Sha256Final(_state, hash);
 45
 8139646        Reset();
 8139647    }
 48
 49    private void Reset()
 50    {
 8719251        _cryptoProvider.Sha256Init(_state);
 8719252    }
 53
 54    #region Dispose Pattern
 55    private void ReleaseUnmanagedResources()
 56    {
 579257        _cryptoProvider.MemoryZero(_state, CryptoConstants.Sha256HashLen);
 579258        _cryptoProvider.MemoryFree(_state);
 579259    }
 60
 61    private void Dispose(bool disposing)
 62    {
 579263        ReleaseUnmanagedResources();
 579264        if (disposing)
 65        {
 574466            _cryptoProvider.Dispose();
 67        }
 579268    }
 69
 70    public void Dispose()
 71    {
 574472        Dispose(true);
 574473        GC.SuppressFinalize(this);
 574474    }
 75
 76    ~Sha256()
 77    {
 4878        Dispose(false);
 9679    }
 80    #endregion
 81}