Hash:GetHashCode()
[NovacoinLibrary.git] / Novacoin / Hash.cs
1 \feff/**
2  *  Novacoin classes library
3  *  Copyright (C) 2015 Alex D. (balthazar.ad@gmail.com)
4
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.
9
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.
14
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/>.
17  */
18
19 using System;
20 using System.Diagnostics.Contracts;
21 using System.Linq;
22
23 namespace Novacoin
24 {
25     public abstract class Hash : IEquatable<Hash>, IComparable<Hash>
26     {
27         /// <summary>
28         /// Array of digest bytes.
29         /// </summary>
30         protected byte[] _hashBytes = null;
31
32         /// <summary>
33         /// Hash size, must be overriden
34         /// </summary>
35         public abstract int hashSize 
36         {
37             get; 
38         }
39
40         /// <summary>
41         /// Initializes an empty instance of the Hash class.
42         /// </summary>
43         public Hash()
44         {
45             _hashBytes = new byte[hashSize];
46         }
47
48         /// <summary>
49         /// Initializes a new instance of Hash class
50         /// </summary>
51         /// <param name="bytesList">Array of bytes</param>
52         public Hash(byte[] bytes, int offset = 0)
53         {
54             if (bytes.Length - offset < hashSize)
55             {
56                 throw new ArgumentException("You need to provide a sufficient amount of data to initialize new instance of hash object.");
57             }
58
59             _hashBytes = new byte[hashSize];
60             Array.Copy(bytes, offset, _hashBytes, 0, hashSize);
61         }
62
63         /// <summary>
64         /// Initializes a new instance of Hash class as a copy of another one
65         /// </summary>
66         /// <param name="bytesList">Instance of hash class</param>
67         public Hash(Hash h)
68         {
69             _hashBytes = new byte[h.hashSize];
70             h._hashBytes.CopyTo(_hashBytes, 0);
71         }
72
73         public bool IsZero
74         {
75             get { return !_hashBytes.Any(b => b != 0); }
76         }
77
78         public static implicit operator byte[](Hash h)
79         {
80             return h._hashBytes;
81         }
82
83         public bool Equals(Hash item)
84         {
85             Contract.Requires<NullReferenceException>((object)item != null, "Null reference is not allowed.");
86             return _hashBytes.SequenceEqual((byte[])item);
87         }
88
89         public override bool Equals(object o)
90         {
91             Contract.Requires<NullReferenceException>(o != null, "Null reference is not allowed.");
92             return _hashBytes.SequenceEqual(((Hash)o)._hashBytes);
93         }
94
95         public override int GetHashCode()
96         {
97             int hash = 17;
98             unchecked
99             {
100                 foreach (var element in _hashBytes)
101                 {
102                     hash = hash * 31 + element.GetHashCode();
103                 }
104             }
105             return hash;
106         }
107
108         public int CompareTo(Hash item)
109         {
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.");
112
113             if (this > item)
114             {
115                 return 1;
116             }
117             else if (this < item)
118             {
119                 return -1;
120             }
121
122             return 0;
123         }
124
125         public static bool operator <(Hash a, Hash b)
126         {
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.");
129             
130             for (int i = a.hashSize - 1; i >= 0; i--)
131             {
132                 if (a._hashBytes[i] < b._hashBytes[i])
133                     return true;
134                 else if (a._hashBytes[i] > b._hashBytes[i])
135                     return false;
136             }
137
138             return false;
139         }
140
141         public static bool operator <=(Hash a, Hash b)
142         {
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.");
145
146             for (int i = a.hashSize - 1; i >= 0; i--)
147             {
148                 if (a._hashBytes[i] < b._hashBytes[i])
149                     return true;
150                 else if (a._hashBytes[i] > b._hashBytes[i])
151                     return false;
152             }
153
154             return false;
155         }
156
157         public static bool operator >(Hash a, Hash b)
158         {
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.");
161
162             for (int i = a.hashSize - 1; i >= 0; i--)
163             {
164                 if (a._hashBytes[i] > b._hashBytes[i])
165                     return true;
166                 else if (a._hashBytes[i] < b._hashBytes[i])
167                     return false;
168             }
169
170             return false;
171         }
172
173         public static bool operator >=(Hash a, Hash b)
174         {
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.");
177
178             for (int i = a.hashSize - 1; i >= 0; i--)
179             {
180                 if (a._hashBytes[i] > b._hashBytes[i])
181                     return true;
182                 else if (a._hashBytes[i] < b._hashBytes[i])
183                     return false;
184             }
185
186             return true;
187         }
188
189         public static bool operator ==(Hash a, Hash b)
190         {
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.");
193
194             return a._hashBytes.SequenceEqual(b._hashBytes);
195         }
196
197         public static bool operator !=(Hash a, Hash b)
198         {
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.");
201
202             return !a.Equals(b);
203         }
204
205         public override string ToString()
206         {
207             return Interop.ToHex(Interop.ReverseBytes(_hashBytes));
208         }
209     }
210 }