Hash comparison operations
[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             if (item == null)
86             {
87                 return false;
88             }
89
90             return _hashBytes.SequenceEqual((byte[])item);
91         }
92
93         public override bool Equals(object o)
94         {
95             throw new NotSupportedException();
96         }
97
98         public override int GetHashCode()
99         {
100             throw new NotSupportedException();
101         }
102
103         public int CompareTo(Hash item)
104         {
105             Contract.Requires<ArgumentException>(item.hashSize == hashSize, "Hashes must have the same size.");
106             Contract.Requires<ArgumentException>(item != null, "Null reference is not allowed.");
107
108             if (this > item)
109             {
110                 return 1;
111             }
112             else if (this < item)
113             {
114                 return -1;
115             }
116
117             return 0;
118         }
119
120         public static bool operator <(Hash a, Hash b)
121         {
122             Contract.Requires<ArgumentException>(a.hashSize == b.hashSize, "Hashes must have the same size.");
123             
124             for (int i = a.hashSize - 1; i >= 0; i--)
125             {
126                 if (a._hashBytes[i] < b._hashBytes[i])
127                     return true;
128                 else if (a._hashBytes[i] > b._hashBytes[i])
129                     return false;
130             }
131
132             return false;
133         }
134
135         public static bool operator <=(Hash a, Hash b)
136         {
137             Contract.Requires<ArgumentException>(a.hashSize == b.hashSize, "Hashes must have the same size.");
138
139             for (int i = a.hashSize - 1; i >= 0; i--)
140             {
141                 if (a._hashBytes[i] < b._hashBytes[i])
142                     return true;
143                 else if (a._hashBytes[i] > b._hashBytes[i])
144                     return false;
145             }
146
147             return false;
148         }
149
150         public static bool operator >(Hash a, Hash b)
151         {
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                     return true;
158                 else if (a._hashBytes[i] < b._hashBytes[i])
159                     return false;
160             }
161
162             return false;
163         }
164
165         public static bool operator >=(Hash a, Hash b)
166         {
167             Contract.Requires<ArgumentException>(a.hashSize == b.hashSize, "Hashes must have the same size.");
168
169             for (int i = a.hashSize - 1; i >= 0; i--)
170             {
171                 if (a._hashBytes[i] > b._hashBytes[i])
172                     return true;
173                 else if (a._hashBytes[i] < b._hashBytes[i])
174                     return false;
175             }
176
177             return true;
178         }
179
180         public static bool operator ==(Hash a, Hash b)
181         {
182             Contract.Requires<ArgumentException>(a.hashSize == b.hashSize, "Hashes must have the same size.");
183
184             return a._hashBytes.SequenceEqual(b._hashBytes);
185         }
186
187         public static bool operator !=(Hash a, Hash b)
188         {
189             Contract.Requires<ArgumentException>(a.hashSize == b.hashSize, "Hashes must have the same size.");
190
191             return !a._hashBytes.SequenceEqual(b._hashBytes);
192         }
193
194         public override string ToString()
195         {
196             return Interop.ToHex(Interop.ReverseBytes(_hashBytes));
197         }
198     }
199 }