Remove RIPEMD160, SHA1 and SHA256 classes.
[NovacoinLibrary.git] / Novacoin / Hash.cs
index 92ce2c7..5b1d3b7 100644 (file)
@@ -27,7 +27,7 @@ namespace Novacoin
         /// <summary>
         /// Array of digest bytes.
         /// </summary>
-        protected byte[] _hashBytes = null;
+        protected byte[] _hashBytes;
 
         /// <summary>
         /// Hash size, must be overriden
@@ -82,29 +82,38 @@ namespace Novacoin
 
         public bool Equals(Hash item)
         {
-            if (item == null)
+            if ((object)item == null)
             {
                 return false;
             }
-
             return _hashBytes.SequenceEqual((byte[])item);
         }
 
         public override bool Equals(object o)
         {
-            throw new NotSupportedException();
+            if (o == null)
+            {
+                return false;
+            }
+
+            return _hashBytes.SequenceEqual(((Hash)o)._hashBytes);
         }
 
         public override int GetHashCode()
         {
-            throw new NotSupportedException();
+            int hash = 17;
+            unchecked
+            {
+                foreach (var element in _hashBytes)
+                {
+                    hash = hash * 31 + element.GetHashCode();
+                }
+            }
+            return hash;
         }
 
         public int CompareTo(Hash item)
         {
-            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)
             {
                 return 1;
@@ -119,14 +128,19 @@ 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 - 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;
@@ -134,14 +148,19 @@ 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 - 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;
@@ -149,14 +168,19 @@ 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 - 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;
@@ -164,14 +188,19 @@ 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 - 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;
@@ -179,6 +208,7 @@ 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.");
 
             return a._hashBytes.SequenceEqual(b._hashBytes);
@@ -186,9 +216,7 @@ namespace Novacoin
 
         public static bool operator !=(Hash a, Hash b)
         {
-            Contract.Requires<ArgumentException>(a.hashSize == b.hashSize, "Hashes must have the same size.");
-
-            return !a._hashBytes.SequenceEqual(b._hashBytes);
+            return !(a == b);
         }
 
         public override string ToString()