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()
100 foreach (var element in _hashBytes)
102 hash = hash * 31 + element.GetHashCode();
108 public int CompareTo(Hash item)
110 Contract.Requires<NullReferenceException>((object)item != null, "Null reference is not allowed.");
111 Contract.Requires<ArgumentException>(item.hashSize == hashSize, "Hashes must have the same size.");
117 else if (this < item)
125 public static bool operator <(Hash a, Hash b)
127 Contract.Requires<NullReferenceException>((object)a != null && (object)b != null, "Null references are not allowed.");
128 Contract.Requires<ArgumentException>(a.hashSize == b.hashSize, "Hashes must have the same size.");
130 for (int i = a.hashSize - 1; i >= 0; i--)
132 if (a._hashBytes[i] < b._hashBytes[i])
134 else if (a._hashBytes[i] > b._hashBytes[i])
141 public static bool operator <=(Hash a, Hash b)
143 Contract.Requires<NullReferenceException>((object)a != null && (object)b != null, "Null references are not allowed.");
144 Contract.Requires<ArgumentException>(a.hashSize == b.hashSize, "Hashes must have the same size.");
146 for (int i = a.hashSize - 1; i >= 0; i--)
148 if (a._hashBytes[i] < b._hashBytes[i])
150 else if (a._hashBytes[i] > b._hashBytes[i])
157 public static bool operator >(Hash a, Hash b)
159 Contract.Requires<NullReferenceException>((object)a != null && (object)b != null, "Null references are not allowed.");
160 Contract.Requires<ArgumentException>(a.hashSize == b.hashSize, "Hashes must have the same size.");
162 for (int i = a.hashSize - 1; i >= 0; i--)
164 if (a._hashBytes[i] > b._hashBytes[i])
166 else if (a._hashBytes[i] < b._hashBytes[i])
173 public static bool operator >=(Hash a, Hash b)
175 Contract.Requires<NullReferenceException>((object)a != null && (object)b != null, "Null references are not allowed.");
176 Contract.Requires<ArgumentException>(a.hashSize == b.hashSize, "Hashes must have the same size.");
178 for (int i = a.hashSize - 1; i >= 0; i--)
180 if (a._hashBytes[i] > b._hashBytes[i])
182 else if (a._hashBytes[i] < b._hashBytes[i])
189 public static bool operator ==(Hash a, Hash b)
191 Contract.Requires<NullReferenceException>((object)a != null && (object)b != null, "Null references are not allowed.");
192 Contract.Requires<ArgumentException>(a.hashSize == b.hashSize, "Hashes must have the same size.");
194 return a._hashBytes.SequenceEqual(b._hashBytes);
197 public static bool operator !=(Hash a, Hash b)
199 Contract.Requires<NullReferenceException>((object)a != null && (object)b != null, "Null references are not allowed.");
200 Contract.Requires<ArgumentException>(a.hashSize == b.hashSize, "Hashes must have the same size.");
205 public override string ToString()
207 return Interop.ToHex(Interop.ReverseBytes(_hashBytes));