Hashsum by Tyrrrz

<PackageReference Include="Hashsum" Version="1.2.1" />

 ChecksumBuilder

Fluent interface for mutating and calculating checksums.
using Hashsum.Internal; using Hashsum.Models; using System; using System.Globalization; using System.IO; using System.Security.Cryptography; using System.Text; namespace Hashsum { public class ChecksumBuilder : IChecksumBuilder { private readonly StringBuilder _buffer; public ChecksumBuilder() { _buffer = new StringBuilder(); } private ChecksumBuilder AppendToBuffer(string value) { _buffer.Append(value); _buffer.Append(';'); return this; } private ChecksumBuilder AppendToBuffer(IFormattable value, string format = null) { string value2 = value.ToString(format, CultureInfo.InvariantCulture); return AppendToBuffer(value2); } public IChecksumBuilder Mutate(string value) { value.GuardNotNull("value"); return AppendToBuffer(value); } public IChecksumBuilder Mutate(char value) { string value2 = value.ToString(); return Mutate(value2); } public IChecksumBuilder Mutate(IFormattable value) { value.GuardNotNull("value"); return AppendToBuffer(value, null); } public IChecksumBuilder Mutate(bool value) { string value2 = value ? "TRUE" : "FALSE"; return Mutate(value2); } public IChecksumBuilder Mutate(TimeSpan value) { long ticks = value.Ticks; return Mutate(ticks); } public IChecksumBuilder Mutate(DateTime value) { long ticks = value.ToUniversalTime().Ticks; return Mutate(ticks); } public IChecksumBuilder Mutate(DateTimeOffset value) { long ticks = value.ToUniversalTime().Ticks; return Mutate(ticks); } public IChecksumBuilder Mutate(byte[] data) { data.GuardNotNull("data"); string value = Convert.ToBase64String(data); return Mutate(value); } public IChecksumBuilder Mutate(Stream stream) { stream.GuardNotNull("stream"); using (MemoryStream memoryStream = new MemoryStream()) { stream.CopyTo(memoryStream); return Mutate(memoryStream.ToArray()); } } public Checksum Calculate(HashAlgorithm algorithm, bool disposeAlgorithm = true) { algorithm.GuardNotNull("algorithm"); try { byte[] bytes = Encoding.Unicode.GetBytes(_buffer.ToString()); _buffer.Clear(); return new Checksum(algorithm.ComputeHash(bytes)); } finally { if (disposeAlgorithm) algorithm.Dispose(); } } public Checksum Calculate() { return Calculate(SHA256.Create(), true); } public override string ToString() { return _buffer.ToString(); } } }