Comment
[NovacoinLibrary.git] / Novacoin / Hash.cs
index 092db18..822cadd 100644 (file)
@@ -82,18 +82,26 @@ namespace Novacoin
 
         public bool Equals(Hash item)
         {
-            if (item == null)
-            {
-                return false;
-            }
-
+            Contract.Requires<NullReferenceException>((object)item != null, "Null reference is not allowed.");
             return _hashBytes.SequenceEqual((byte[])item);
         }
 
+        public override bool Equals(object o)
+        {
+            Contract.Requires<NullReferenceException>(o != null, "Null reference is not allowed.");
+            return _hashBytes.SequenceEqual(((Hash)o)._hashBytes);
+        }
+
+        public override int GetHashCode()
+        {
+            // It's a hash already, so any idea of trying to get its hashcode doesn't make any sense to me
+            throw new NotSupportedException();
+        }
+
         public int CompareTo(Hash item)
         {
+            Contract.Requires<NullReferenceException>((object)item != null, "Null reference is not allowed.");
             Contract.Requires<ArgumentException>(item.hashSize == hashSize, "Hashes must have the same size.");
-            Contract.Requires<ArgumentException>(item != null, "Null reference is not allowed.");
 
             if (this > item)
             {
@@ -109,9 +117,10 @@ namespace Novacoin
 
         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; i >= 0; i--)
+            for (int i = a.hashSize - 1; i >= 0; i--)
             {
                 if (a._hashBytes[i] < b._hashBytes[i])
                     return true;
@@ -124,9 +133,10 @@ namespace Novacoin
 
         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; i >= 0; i--)
+            for (int i = a.hashSize - 1; i >= 0; i--)
             {
                 if (a._hashBytes[i] < b._hashBytes[i])
                     return true;
@@ -139,9 +149,10 @@ namespace Novacoin
 
         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; i >= 0; i--)
+            for (int i = a.hashSize - 1; i >= 0; i--)
             {
                 if (a._hashBytes[i] > b._hashBytes[i])
                     return true;
@@ -154,9 +165,10 @@ namespace Novacoin
 
         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; i >= 0; i--)
+            for (int i = a.hashSize - 1; i >= 0; i--)
             {
                 if (a._hashBytes[i] > b._hashBytes[i])
                     return true;
@@ -167,6 +179,22 @@ namespace Novacoin
             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)
+        {
+            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.Equals(b);
+        }
+
         public override string ToString()
         {
             return Interop.ToHex(Interop.ReverseBytes(_hashBytes));