Comment
[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             // It's a hash already, so any idea of trying to get its hashcode doesn't make any sense to me
98             throw new NotSupportedException();
99         }
100
101         public int CompareTo(Hash item)
102         {
103             Contract.Requires<NullReferenceException>((object)item != null, "Null reference is not allowed.");
104             Contract.Requires<ArgumentException>(item.hashSize == hashSize, "Hashes must have the same size.");
105
106             if (this > item)
107             {
108                 return 1;
109             }
110             else if (this < item)
111             {
112                 return -1;
113             }
114
115             return 0;
116         }
117
118         public static bool operator <(Hash a, Hash b)
119         {
120             Contract.Requires<NullReferenceException>((object)a != null && (object)b != null, "Null references are not allowed.");
121             Contract.Requires<ArgumentException>(a.hashSize == b.hashSize, "Hashes must have the same size.");
122             
123             for (int i = a.hashSize - 1; i >= 0; i--)
124             {
125                 if (a._hashBytes[i] < b._hashBytes[i])
126                     return true;
127                 else if (a._hashBytes[i] > b._hashBytes[i])
128                     return false;
129             }
130
131             return false;
132         }
133
134         public static bool operator <=(Hash a, Hash b)
135         {
136             Contract.Requires<NullReferenceException>((object)a != null && (object)b != null, "Null references are not allowed.");
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<NullReferenceException>((object)a != null && (object)b != null, "Null references are not allowed.");
153             Contract.Requires<ArgumentException>(a.hashSize == b.hashSize, "Hashes must have the same size.");
154
155             for (int i = a.hashSize - 1; i >= 0; i--)
156             {
157                 if (a._hashBytes[i] > b._hashBytes[i])
158                     return true;
159                 else if (a._hashBytes[i] < b._hashBytes[i])
160                     return false;
161             }
162
163             return false;
164         }
165
166         public static bool operator >=(Hash a, Hash b)
167         {
168             Contract.Requires<NullReferenceException>((object)a != null && (object)b != null, "Null references are not allowed.");
169             Contract.Requires<ArgumentException>(a.hashSize == b.hashSize, "Hashes must have the same size.");
170
171             for (int i = a.hashSize - 1; i >= 0; i--)
172             {
173                 if (a._hashBytes[i] > b._hashBytes[i])
174                     return true;
175                 else if (a._hashBytes[i] < b._hashBytes[i])
176                     return false;
177             }
178
179             return true;
180         }
181
182         public static bool operator ==(Hash a, Hash b)
183         {
184             Contract.Requires<NullReferenceException>((object)a != null && (object)b != null, "Null references are not allowed.");
185             Contract.Requires<ArgumentException>(a.hashSize == b.hashSize, "Hashes must have the same size.");
186
187             return a._hashBytes.SequenceEqual(b._hashBytes);
188         }
189
190         public static bool operator !=(Hash a, Hash b)
191         {
192             Contract.Requires<NullReferenceException>((object)a != null && (object)b != null, "Null references are not allowed.");
193             Contract.Requires<ArgumentException>(a.hashSize == b.hashSize, "Hashes must have the same size.");
194
195             return !a.Equals(b);
196         }
197
198         public override string ToString()
199         {
200             return Interop.ToHex(Interop.ReverseBytes(_hashBytes));
201         }
202     }
203 }