2 * Novacoin classes library
3 * Copyright (C) 2015 Alex D. (balthazar.ad@gmail.com)
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU Affero General Public License as
7 * published by the Free Software Foundation, either version 3 of the
8 * License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU Affero General Public License for more details.
15 * You should have received a copy of the GNU Affero General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 using System.Diagnostics.Contracts;
25 public abstract class Hash : IEquatable<Hash>, IComparable<Hash>
28 /// Array of digest bytes.
30 protected byte[] _hashBytes = null;
33 /// Hash size, must be overriden
35 public abstract int hashSize
41 /// Initializes an empty instance of the Hash class.
45 _hashBytes = new byte[hashSize];
49 /// Initializes a new instance of Hash class
51 /// <param name="bytesList">Array of bytes</param>
52 public Hash(byte[] bytes, int offset = 0)
54 if (bytes.Length - offset < hashSize)
56 throw new ArgumentException("You need to provide a sufficient amount of data to initialize new instance of hash object.");
59 _hashBytes = new byte[hashSize];
60 Array.Copy(bytes, offset, _hashBytes, 0, hashSize);
64 /// Initializes a new instance of Hash class as a copy of another one
66 /// <param name="bytesList">Instance of hash class</param>
69 _hashBytes = new byte[h.hashSize];
70 h._hashBytes.CopyTo(_hashBytes, 0);
75 get { return !_hashBytes.Any(b => b != 0); }
78 public static implicit operator byte[](Hash h)
83 public bool Equals(Hash item)
85 Contract.Requires<NullReferenceException>((object)item != null, "Null reference is not allowed.");
86 return _hashBytes.SequenceEqual((byte[])item);
89 public override bool Equals(object o)
91 Contract.Requires<NullReferenceException>(o != null, "Null reference is not allowed.");
92 return _hashBytes.SequenceEqual(((Hash)o)._hashBytes);
95 public override int GetHashCode()
97 // It's a hash already, so any idea of trying to get its hashcode doesn't make any sense to me
98 throw new NotSupportedException();
101 public int CompareTo(Hash item)
103 Contract.Requires<NullReferenceException>((object)item != null, "Null reference is not allowed.");
104 Contract.Requires<ArgumentException>(item.hashSize == hashSize, "Hashes must have the same size.");
110 else if (this < item)
118 public static bool operator <(Hash a, Hash b)
120 Contract.Requires<NullReferenceException>((object)a != null && (object)b != null, "Null references are not allowed.");
121 Contract.Requires<ArgumentException>(a.hashSize == b.hashSize, "Hashes must have the same size.");
123 for (int i = a.hashSize - 1; i >= 0; i--)
125 if (a._hashBytes[i] < b._hashBytes[i])
127 else if (a._hashBytes[i] > b._hashBytes[i])
134 public static bool operator <=(Hash a, Hash b)
136 Contract.Requires<NullReferenceException>((object)a != null && (object)b != null, "Null references are not allowed.");
137 Contract.Requires<ArgumentException>(a.hashSize == b.hashSize, "Hashes must have the same size.");
139 for (int i = a.hashSize - 1; i >= 0; i--)
141 if (a._hashBytes[i] < b._hashBytes[i])
143 else if (a._hashBytes[i] > b._hashBytes[i])
150 public static bool operator >(Hash a, Hash b)
152 Contract.Requires<NullReferenceException>((object)a != null && (object)b != null, "Null references are not allowed.");
153 Contract.Requires<ArgumentException>(a.hashSize == b.hashSize, "Hashes must have the same size.");
155 for (int i = a.hashSize - 1; i >= 0; i--)
157 if (a._hashBytes[i] > b._hashBytes[i])
159 else if (a._hashBytes[i] < b._hashBytes[i])
166 public static bool operator >=(Hash a, Hash b)
168 Contract.Requires<NullReferenceException>((object)a != null && (object)b != null, "Null references are not allowed.");
169 Contract.Requires<ArgumentException>(a.hashSize == b.hashSize, "Hashes must have the same size.");
171 for (int i = a.hashSize - 1; i >= 0; i--)
173 if (a._hashBytes[i] > b._hashBytes[i])
175 else if (a._hashBytes[i] < b._hashBytes[i])
182 public static bool operator ==(Hash a, Hash b)
184 Contract.Requires<NullReferenceException>((object)a != null && (object)b != null, "Null references are not allowed.");
185 Contract.Requires<ArgumentException>(a.hashSize == b.hashSize, "Hashes must have the same size.");
187 return a._hashBytes.SequenceEqual(b._hashBytes);
190 public static bool operator !=(Hash a, Hash b)
192 Contract.Requires<NullReferenceException>((object)a != null && (object)b != null, "Null references are not allowed.");
193 Contract.Requires<ArgumentException>(a.hashSize == b.hashSize, "Hashes must have the same size.");
198 public override string ToString()
200 return Interop.ToHex(Interop.ReverseBytes(_hashBytes));