X-Git-Url: https://git.novaco.in/?a=blobdiff_plain;f=Novacoin%2FHash.cs;h=5b1d3b74335030e8b2c9ceb251352410600ae35f;hb=1f6b6ffe24de703593f20f5f881a2e797473621c;hp=990ec7bf543b0d7fc44724bc110b681520c6373c;hpb=1edac525d07f68f781fea5d5fcc91c548623d912;p=NovacoinLibrary.git diff --git a/Novacoin/Hash.cs b/Novacoin/Hash.cs index 990ec7b..5b1d3b7 100644 --- a/Novacoin/Hash.cs +++ b/Novacoin/Hash.cs @@ -17,23 +17,17 @@ */ using System; -using System.Security.Cryptography; -using System.Collections.Generic; +using System.Diagnostics.Contracts; using System.Linq; namespace Novacoin { - public abstract class Hash + public abstract class Hash : IEquatable, IComparable { /// - /// Computes the SHA256 hash for the input data using the managed library. - /// - protected static SHA256Managed _hasher256 = new SHA256Managed(); - - /// /// Array of digest bytes. /// - protected byte[] _hashBytes = null; + protected byte[] _hashBytes; /// /// Hash size, must be overriden @@ -43,26 +37,12 @@ namespace Novacoin get; } - public byte[] hashBytes - { - get { return _hashBytes; } - } - /// /// Initializes an empty instance of the Hash class. /// public Hash() { - _hashBytes = Enumerable.Repeat(0, hashSize).ToArray(); - } - - /// - /// Initializes a new instance of Hash class - /// - /// List of bytes - public Hash(IEnumerable bytes, int skip = 0) - { - _hashBytes = bytes.Skip(skip).Take(hashSize).ToArray(); + _hashBytes = new byte[hashSize]; } /// @@ -71,6 +51,11 @@ namespace Novacoin /// Array of bytes public Hash(byte[] bytes, int offset = 0) { + if (bytes.Length - offset < hashSize) + { + throw new ArgumentException("You need to provide a sufficient amount of data to initialize new instance of hash object."); + } + _hashBytes = new byte[hashSize]; Array.Copy(bytes, offset, _hashBytes, 0, hashSize); } @@ -90,6 +75,150 @@ namespace Novacoin get { return !_hashBytes.Any(b => b != 0); } } + public static implicit operator byte[](Hash h) + { + return h._hashBytes; + } + + public bool Equals(Hash item) + { + if ((object)item == null) + { + return false; + } + return _hashBytes.SequenceEqual((byte[])item); + } + + public override bool Equals(object o) + { + if (o == null) + { + return false; + } + + return _hashBytes.SequenceEqual(((Hash)o)._hashBytes); + } + + public override int GetHashCode() + { + int hash = 17; + unchecked + { + foreach (var element in _hashBytes) + { + hash = hash * 31 + element.GetHashCode(); + } + } + return hash; + } + + public int CompareTo(Hash item) + { + if (this > item) + { + return 1; + } + else if (this < item) + { + return -1; + } + + return 0; + } + + public static bool operator <(Hash a, Hash b) + { + Contract.Requires((object)a != null && (object)b != null, "Null references are not allowed."); + Contract.Requires(a.hashSize == b.hashSize, "Hashes must have the same size."); + + for (int i = a.hashSize - 1; i >= 0; i--) + { + if (a._hashBytes[i] < b._hashBytes[i]) + { + return true; + } + else if (a._hashBytes[i] > b._hashBytes[i]) + { + return false; + } + } + + return false; + } + + public static bool operator <=(Hash a, Hash b) + { + Contract.Requires((object)a != null && (object)b != null, "Null references are not allowed."); + Contract.Requires(a.hashSize == b.hashSize, "Hashes must have the same size."); + + for (int i = a.hashSize - 1; i >= 0; i--) + { + if (a._hashBytes[i] < b._hashBytes[i]) + { + return true; + } + else if (a._hashBytes[i] > b._hashBytes[i]) + { + return false; + } + } + + return false; + } + + public static bool operator >(Hash a, Hash b) + { + Contract.Requires((object)a != null && (object)b != null, "Null references are not allowed."); + Contract.Requires(a.hashSize == b.hashSize, "Hashes must have the same size."); + + for (int i = a.hashSize - 1; i >= 0; i--) + { + if (a._hashBytes[i] > b._hashBytes[i]) + { + return true; + } + else if (a._hashBytes[i] < b._hashBytes[i]) + { + return false; + } + } + + return false; + } + + public static bool operator >=(Hash a, Hash b) + { + Contract.Requires((object)a != null && (object)b != null, "Null references are not allowed."); + Contract.Requires(a.hashSize == b.hashSize, "Hashes must have the same size."); + + for (int i = a.hashSize - 1; i >= 0; i--) + { + if (a._hashBytes[i] > b._hashBytes[i]) + { + return true; + } + else if (a._hashBytes[i] < b._hashBytes[i]) + { + return false; + } + } + + return true; + } + + public static bool operator ==(Hash a, Hash b) + { + Contract.Requires((object)a != null && (object)b != null, "Null references are not allowed."); + Contract.Requires(a.hashSize == b.hashSize, "Hashes must have the same size."); + + return a._hashBytes.SequenceEqual(b._hashBytes); + } + + public static bool operator !=(Hash a, Hash b) + { + return !(a == b); + } + public override string ToString() { return Interop.ToHex(Interop.ReverseBytes(_hashBytes));