Remove RIPEMD160, SHA1 and SHA256 classes.
[NovacoinLibrary.git] / Novacoin / Hash.cs
index 05c4567..5b1d3b7 100644 (file)
  */
 
 using System;
+using System.Diagnostics.Contracts;
 using System.Linq;
 
 namespace Novacoin
 {
-    public abstract class Hash
+    public abstract class Hash : IEquatable<Hash>, IComparable<Hash>
     {
         /// <summary>
         /// Array of digest bytes.
         /// </summary>
-        protected byte[] _hashBytes = null;
+        protected byte[] _hashBytes;
 
         /// <summary>
         /// Hash size, must be overriden
@@ -79,6 +80,145 @@ namespace Novacoin
             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<NullReferenceException>((object)a != null && (object)b != null, "Null references are not allowed.");
+            Contract.Requires<ArgumentException>(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<NullReferenceException>((object)a != null && (object)b != null, "Null references are not allowed.");
+            Contract.Requires<ArgumentException>(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<NullReferenceException>((object)a != null && (object)b != null, "Null references are not allowed.");
+            Contract.Requires<ArgumentException>(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<NullReferenceException>((object)a != null && (object)b != null, "Null references are not allowed.");
+            Contract.Requires<ArgumentException>(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<NullReferenceException>((object)a != null && (object)b != null, "Null references are not allowed.");
+            Contract.Requires<ArgumentException>(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));