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;
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 if ((object)item == null)
89 return _hashBytes.SequenceEqual((byte[])item);
92 public override bool Equals(object o)
99 return _hashBytes.SequenceEqual(((Hash)o)._hashBytes);
102 public override int GetHashCode()
107 foreach (var element in _hashBytes)
109 hash = hash * 31 + element.GetHashCode();
115 public int CompareTo(Hash item)
121 else if (this < item)
129 public static bool operator <(Hash a, Hash b)
131 Contract.Requires<NullReferenceException>((object)a != null && (object)b != null, "Null references are not allowed.");
132 Contract.Requires<ArgumentException>(a.hashSize == b.hashSize, "Hashes must have the same size.");
134 for (int i = a.hashSize - 1; i >= 0; i--)
136 if (a._hashBytes[i] < b._hashBytes[i])
140 else if (a._hashBytes[i] > b._hashBytes[i])
149 public static bool operator <=(Hash a, Hash b)
151 Contract.Requires<NullReferenceException>((object)a != null && (object)b != null, "Null references are not allowed.");
152 Contract.Requires<ArgumentException>(a.hashSize == b.hashSize, "Hashes must have the same size.");
154 for (int i = a.hashSize - 1; i >= 0; i--)
156 if (a._hashBytes[i] < b._hashBytes[i])
160 else if (a._hashBytes[i] > b._hashBytes[i])
169 public static bool operator >(Hash a, Hash b)
171 Contract.Requires<NullReferenceException>((object)a != null && (object)b != null, "Null references are not allowed.");
172 Contract.Requires<ArgumentException>(a.hashSize == b.hashSize, "Hashes must have the same size.");
174 for (int i = a.hashSize - 1; i >= 0; i--)
176 if (a._hashBytes[i] > b._hashBytes[i])
180 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 for (int i = a.hashSize - 1; i >= 0; i--)
196 if (a._hashBytes[i] > b._hashBytes[i])
200 else if (a._hashBytes[i] < b._hashBytes[i])
209 public static bool operator ==(Hash a, Hash b)
211 Contract.Requires<NullReferenceException>((object)a != null && (object)b != null, "Null references are not allowed.");
212 Contract.Requires<ArgumentException>(a.hashSize == b.hashSize, "Hashes must have the same size.");
214 return a._hashBytes.SequenceEqual(b._hashBytes);
217 public static bool operator !=(Hash a, Hash b)
222 public override string ToString()
224 return Interop.ToHex(Interop.ReverseBytes(_hashBytes));