X-Git-Url: https://git.novaco.in/?a=blobdiff_plain;f=Novacoin%2FHash.cs;h=5b1d3b74335030e8b2c9ceb251352410600ae35f;hb=1f6b6ffe24de703593f20f5f881a2e797473621c;hp=92ce2c75c9af82ff99244f0303c10ea713f89657;hpb=5671ee17bc35fb578e18168833c5d1e3e86a7e28;p=NovacoinLibrary.git diff --git a/Novacoin/Hash.cs b/Novacoin/Hash.cs index 92ce2c7..5b1d3b7 100644 --- a/Novacoin/Hash.cs +++ b/Novacoin/Hash.cs @@ -27,7 +27,7 @@ namespace Novacoin /// /// Array of digest bytes. /// - protected byte[] _hashBytes = null; + protected byte[] _hashBytes; /// /// 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(item.hashSize == hashSize, "Hashes must have the same size."); - Contract.Requires(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((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; @@ -134,14 +148,19 @@ namespace Novacoin 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; @@ -149,14 +168,19 @@ namespace Novacoin 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; @@ -164,14 +188,19 @@ namespace Novacoin 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; @@ -179,6 +208,7 @@ namespace Novacoin 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); @@ -186,9 +216,7 @@ namespace Novacoin public static bool operator !=(Hash a, Hash b) { - Contract.Requires(a.hashSize == b.hashSize, "Hashes must have the same size."); - - return !a._hashBytes.SequenceEqual(b._hashBytes); + return !(a == b); } public override string ToString()