Merkle tree computation
[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.Security.Cryptography;
21 using System.Collections.Generic;
22 using System.Linq;
23
24 using System.Numerics;
25
26 namespace Novacoin
27 {
28     public abstract class Hash
29     {
30         /// <summary>
31         /// Computes the SHA256 hash for the input data using the managed library.
32         /// </summary>
33         protected static SHA256Managed _hasher256 = new SHA256Managed();
34         
35         /// <summary>
36         /// Array of digest bytes.
37         /// </summary>
38         protected byte[] _hashBytes = null;
39
40         /// <summary>
41         /// Hash size, must be overriden
42         /// </summary>
43         public abstract int hashSize 
44         {
45             get; 
46         }
47
48         public byte[] hashBytes
49         {
50             get { return _hashBytes; }
51         }
52
53         /// <summary>
54         /// Initializes an empty instance of the Hash class.
55         /// </summary>
56         public Hash()
57         {
58             _hashBytes = new byte[hashSize];
59         }
60
61         /// <summary>
62         /// Initializes a new instance of Hash class
63         /// </summary>
64         /// <param name="bytesList">List of bytes</param>
65         public Hash(IEnumerable<byte> bytes, int skip = 0)
66         {
67             _hashBytes = bytes.Skip(skip).Take(hashSize).ToArray();
68         }
69
70         /// <summary>
71         /// Initializes a new instance of Hash class
72         /// </summary>
73         /// <param name="bytesList">Array of bytes</param>
74         public Hash(byte[] bytes, int offset = 0)
75         {
76             _hashBytes = new byte[hashSize];
77             Array.Copy(bytes, offset, _hashBytes, 0, hashSize);
78         }
79
80         /// <summary>
81         /// Initializes a new instance of Hash class as a copy of another one
82         /// </summary>
83         /// <param name="bytesList">Instance of hash class</param>
84         public Hash(Hash h)
85         {
86             _hashBytes = new byte[h.hashSize];
87             h._hashBytes.CopyTo(_hashBytes, 0);
88         }
89
90         public bool IsZero
91         {
92             get { return !_hashBytes.Any(b => b != 0); }
93         }
94
95         /*public static implicit operator BigInteger(Hash h)
96         {
97             return new BigInteger(h._hashBytes);
98         }*/
99
100         public override string ToString()
101         {
102             return Interop.ToHex(Interop.ReverseBytes(_hashBytes));
103         }
104     }
105 }