5b1d3b74335030e8b2c9ceb251352410600ae35f
[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;
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             if ((object)item == null)
86             {
87                 return false;
88             }
89             return _hashBytes.SequenceEqual((byte[])item);
90         }
91
92         public override bool Equals(object o)
93         {
94             if (o == null)
95             {
96                 return false;
97             }
98
99             return _hashBytes.SequenceEqual(((Hash)o)._hashBytes);
100         }
101
102         public override int GetHashCode()
103         {
104             int hash = 17;
105             unchecked
106             {
107                 foreach (var element in _hashBytes)
108                 {
109                     hash = hash * 31 + element.GetHashCode();
110                 }
111             }
112             return hash;
113         }
114
115         public int CompareTo(Hash item)
116         {
117             if (this > item)
118             {
119                 return 1;
120             }
121             else if (this < item)
122             {
123                 return -1;
124             }
125
126             return 0;
127         }
128
129         public static bool operator <(Hash a, Hash b)
130         {
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.");
133             
134             for (int i = a.hashSize - 1; i >= 0; i--)
135             {
136                 if (a._hashBytes[i] < b._hashBytes[i])
137                 {
138                     return true;
139                 }
140                 else if (a._hashBytes[i] > b._hashBytes[i])
141                 {
142                     return false;
143                 }
144             }
145
146             return false;
147         }
148
149         public static bool operator <=(Hash a, Hash b)
150         {
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.");
153
154             for (int i = a.hashSize - 1; i >= 0; i--)
155             {
156                 if (a._hashBytes[i] < b._hashBytes[i])
157                 {
158                     return true;
159                 }
160                 else if (a._hashBytes[i] > b._hashBytes[i])
161                 {
162                     return false;
163                 }
164             }
165
166             return false;
167         }
168
169         public static bool operator >(Hash a, Hash b)
170         {
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.");
173
174             for (int i = a.hashSize - 1; i >= 0; i--)
175             {
176                 if (a._hashBytes[i] > b._hashBytes[i])
177                 {
178                     return true;
179                 }
180                 else if (a._hashBytes[i] < b._hashBytes[i])
181                 {
182                     return false;
183                 }
184             }
185
186             return false;
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             for (int i = a.hashSize - 1; i >= 0; i--)
195             {
196                 if (a._hashBytes[i] > b._hashBytes[i])
197                 {
198                     return true;
199                 }
200                 else if (a._hashBytes[i] < b._hashBytes[i])
201                 {
202                     return false;
203                 }
204             }
205
206             return true;
207         }
208
209         public static bool operator ==(Hash a, Hash b)
210         {
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.");
213
214             return a._hashBytes.SequenceEqual(b._hashBytes);
215         }
216
217         public static bool operator !=(Hash a, Hash b)
218         {
219             return !(a == b);
220         }
221
222         public override string ToString()
223         {
224             return Interop.ToHex(Interop.ReverseBytes(_hashBytes));
225         }
226     }
227 }