2 using System.Security.Cryptography;
3 using System.Collections.Generic;
8 public abstract class Hash
11 /// Computes the SHA256 hash for the input data using the managed library.
13 protected static SHA256Managed _hasher256 = new SHA256Managed();
16 /// Array of digest bytes.
18 protected byte[] _hashBytes = null;
21 /// Hash size, must be overriden
23 public abstract int hashSize
28 public byte[] hashBytes
30 get { return _hashBytes; }
34 /// Initializes an empty instance of the Hash class.
38 _hashBytes = Enumerable.Repeat<byte>(0, hashSize).ToArray();
42 /// Initializes a new instance of Hash class with first 20 bytes from supplied list
44 /// <param name="bytesList">List of bytes</param>
45 public Hash(IEnumerable<byte> bytes, int skip = 0)
47 _hashBytes = bytes.Skip(skip).Take(hashSize).ToArray();
50 public Hash(byte[] bytes, int offset = 0)
52 _hashBytes = new byte[hashSize];
53 Array.Copy(bytes, offset, _hashBytes, 0, hashSize);
58 _hashBytes = new byte[h.hashSize];
59 h._hashBytes.CopyTo(_hashBytes, 0);
64 get { return !_hashBytes.Any(b => b != 0); }
67 public override string ToString()
69 return Interop.ToHex(Interop.ReverseBytes(_hashBytes));