< 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: 30_15166811759
Line coverage
92%
Covered lines: 23
Uncovered lines: 2
Coverable lines: 25
Total lines: 80
Line coverage: 92%
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%210%

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 Factories;
 7using Interfaces;
 8
 9/// <summary>
 10/// SHA-256 from <see href="https://nvlpubs.nist.gov/nistpubs/FIPS/NIST.FIPS.180-4.pdf">FIPS 180-4</see>.
 11/// </summary>
 12public sealed class Sha256 : IDisposable
 13{
 14    private readonly ICryptoProvider _cryptoProvider;
 15    private readonly IntPtr _state;
 16
 632817    public Sha256()
 18    {
 632819        _cryptoProvider = CryptoFactory.GetCryptoProvider();
 632820        _state = _cryptoProvider.MemoryAlloc(CryptoConstants.LIBSODIUM_SHA256_STATE_LEN);
 632821        Reset();
 632822    }
 23
 24    /// <summary>
 25    /// Appends the specified data to the data already processed in the hash.
 26    /// </summary>
 27    public void AppendData(ReadOnlySpan<byte> data)
 28    {
 8458829        if (!data.IsEmpty)
 30        {
 8409231            _cryptoProvider.Sha256Update(_state, data);
 32        }
 8458833    }
 34
 35    /// <summary>
 36    /// Retrieves the hash for the accumulated data into the hash parameter,
 37    /// and resets the object to its initial state.
 38    /// </summary>
 39    public void GetHashAndReset(Span<byte> hash)
 40    {
 41        Debug.Assert(hash.Length == CryptoConstants.SHA256_HASH_LEN);
 42
 8167243        _cryptoProvider.Sha256Final(_state, hash);
 44
 8167245        Reset();
 8167246    }
 47
 48    private void Reset()
 49    {
 8800050        _cryptoProvider.Sha256Init(_state);
 8800051    }
 52
 53    #region Dispose Pattern
 54    private void ReleaseUnmanagedResources()
 55    {
 628056        _cryptoProvider.MemoryZero(_state, CryptoConstants.SHA256_HASH_LEN);
 628057        _cryptoProvider.MemoryFree(_state);
 628058    }
 59
 60    private void Dispose(bool disposing)
 61    {
 628062        ReleaseUnmanagedResources();
 628063        if (disposing)
 64        {
 628065            _cryptoProvider.Dispose();
 66        }
 628067    }
 68
 69    public void Dispose()
 70    {
 628071        Dispose(true);
 628072        GC.SuppressFinalize(this);
 628073    }
 74
 75    ~Sha256()
 76    {
 077        Dispose(false);
 078    }
 79    #endregion
 80}