/// <summary>
/// Array of digest bytes.
/// </summary>
- protected byte[] _hashBytes = null;
+ protected byte[] _hashBytes;
/// <summary>
/// Hash size, must be overriden
public bool Equals(Hash item)
{
- if (item == null)
+ if ((object)item == null)
{
return false;
}
-
return _hashBytes.SequenceEqual((byte[])item);
}
- public int CompareTo(Hash item)
+ public override bool Equals(object o)
{
- Contract.Requires<ArgumentException>(item.hashSize == hashSize, "Hashes must have the same size.");
- Contract.Requires<ArgumentException>(item != null, "Null reference is not allowed.");
+ 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;
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;
+ }
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; i >= 0; i--)
+ 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; i >= 0; i--)
+ 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; i >= 0; i--)
+ 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));