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)
90 return _hashBytes.SequenceEqual((byte[])item);
93 public int CompareTo(Hash item)
95 Contract.Requires<ArgumentException>(item.hashSize == hashSize, "Hashes must have the same size.");
96 Contract.Requires<ArgumentException>(item != null, "Null reference is not allowed.");
102 else if (this < item)
110 public static bool operator <(Hash a, Hash b)
112 Contract.Requires<ArgumentException>(a.hashSize == b.hashSize, "Hashes must have the same size.");
114 for (int i = a.hashSize; i >= 0; i--)
116 if (a._hashBytes[i] < b._hashBytes[i])
118 else if (a._hashBytes[i] > b._hashBytes[i])
125 public static bool operator <=(Hash a, Hash b)
127 Contract.Requires<ArgumentException>(a.hashSize == b.hashSize, "Hashes must have the same size.");
129 for (int i = a.hashSize; i >= 0; i--)
131 if (a._hashBytes[i] < b._hashBytes[i])
133 else if (a._hashBytes[i] > b._hashBytes[i])
140 public static bool operator >(Hash a, Hash b)
142 Contract.Requires<ArgumentException>(a.hashSize == b.hashSize, "Hashes must have the same size.");
144 for (int i = a.hashSize; i >= 0; i--)
146 if (a._hashBytes[i] > b._hashBytes[i])
148 else if (a._hashBytes[i] < b._hashBytes[i])
155 public static bool operator >=(Hash a, Hash b)
157 Contract.Requires<ArgumentException>(a.hashSize == b.hashSize, "Hashes must have the same size.");
159 for (int i = a.hashSize; i >= 0; i--)
161 if (a._hashBytes[i] > b._hashBytes[i])
163 else if (a._hashBytes[i] < b._hashBytes[i])
170 public override string ToString()
172 return Interop.ToHex(Interop.ReverseBytes(_hashBytes));