using System; using System.Security.Cryptography; using RethinkDb.Driver.Utils; namespace RethinkDb.Driver.Net { /// /// Cross platform SHA 256 Hasher /// public class IncrementalSHA256 : IDisposable { #if STANDARD private IncrementalHash hasher; #else private SHA256 hasher; #endif /// /// Constructor /// public IncrementalSHA256() { #if STANDARD hasher = IncrementalHash.CreateHash(HashAlgorithmName.SHA256); #else hasher = SHA256.Create(); #endif } /// /// Updates the hash value /// public void AppendData(byte[] data) { #if STANDARD hasher.AppendData(data); #else hasher.TransformBlock(data, 0, data.Length, null, 0); #endif } /// /// Gets the final hash calculation in hex string. /// public string GetHashStringAndReset() { return StringHelper.GetHexString(GetHashAndReset()); } /// /// Gets the final hash calculation as byte array. /// public byte[] GetHashAndReset() { #if STANDARD return hasher.GetHashAndReset(); #else hasher.TransformFinalBlock(new byte[0], 0, 0); return hasher.Hash; #endif } /// /// Disposes the hasher. /// public void Dispose() { hasher.Dispose(); } } }