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 int CompareTo(Hash item)
94         {
95             Contract.Requires<ArgumentException>(item.hashSize == hashSize, "Hashes must have the same size.");
96             Contract.Requires<ArgumentException>(item != null, "Null reference is not allowed.");
97
98             if (this > item)
99             {
100                 return 1;
101             }
102             else if (this < item)
103             {
104                 return -1;
105             }
106
107             return 0;
108         }
109
110         public static bool operator <(Hash a, Hash b)
111         {
112             Contract.Requires<ArgumentException>(a.hashSize == b.hashSize, "Hashes must have the same size.");
113             
114             for (int i = a.hashSize; i >= 0; i--)
115             {
116                 if (a._hashBytes[i] < b._hashBytes[i])
117                     return true;
118                 else if (a._hashBytes[i] > b._hashBytes[i])
119                     return false;
120             }
121
122             return false;
123         }
124
125         public static bool operator <=(Hash a, Hash b)
126         {
127             Contract.Requires<ArgumentException>(a.hashSize == b.hashSize, "Hashes must have the same size.");
128
129             for (int i = a.hashSize; i >= 0; i--)
130             {
131                 if (a._hashBytes[i] < b._hashBytes[i])
132                     return true;
133                 else if (a._hashBytes[i] > b._hashBytes[i])
134                     return false;
135             }
136
137             return false;
138         }
139
140         public static bool operator >(Hash a, Hash b)
141         {
142             Contract.Requires<ArgumentException>(a.hashSize == b.hashSize, "Hashes must have the same size.");
143
144             for (int i = a.hashSize; i >= 0; i--)
145             {
146                 if (a._hashBytes[i] > b._hashBytes[i])
147                     return true;
148                 else if (a._hashBytes[i] < b._hashBytes[i])
149                     return false;
150             }
151
152             return false;
153         }
154
155         public static bool operator >=(Hash a, Hash b)
156         {
157             Contract.Requires<ArgumentException>(a.hashSize == b.hashSize, "Hashes must have the same size.");
158
159             for (int i = a.hashSize; i >= 0; i--)
160             {
161                 if (a._hashBytes[i] > b._hashBytes[i])
162                     return true;
163                 else if (a._hashBytes[i] < b._hashBytes[i])
164                     return false;
165             }
166
167             return true;
168         }
169
170         public override string ToString()
171         {
172             return Interop.ToHex(Interop.ReverseBytes(_hashBytes));
173         }
174     }
175 }