We're at little-endian only
[NovacoinLibrary.git] / Novacoin / Hash.cs
1 \feffusing System.Security.Cryptography;
2 using System.Collections.Generic;
3 using System.Linq;
4
5 namespace Novacoin
6 {
7     public abstract class Hash
8     {
9         /// <summary>
10         /// Computes the SHA256 hash for the input data using the managed library.
11         /// </summary>
12         protected static SHA256Managed _hasher256 = new SHA256Managed();
13         
14         /// <summary>
15         /// Array of digest bytes.
16         /// </summary>
17         protected byte[] _hashBytes = null;
18
19         /// <summary>
20         /// Hash size, must be overriden
21         /// </summary>
22         public virtual int hashSize 
23         {
24             get; 
25             private set;
26         }
27
28         public byte[] hashBytes
29         {
30             get { return _hashBytes; }
31         }
32
33         /// <summary>
34         /// Initializes an empty instance of the Hash class.
35         /// </summary>
36         public Hash()
37         {
38             _hashBytes = Enumerable.Repeat<byte>(0, hashSize).ToArray();
39         }
40
41         /// <summary>
42         /// Initializes a new instance of Hash class with first 20 bytes from supplied list
43         /// </summary>
44         /// <param name="bytesList">List of bytes</param>
45         public Hash(IList<byte> bytesList)
46         {
47             _hashBytes = bytesList.Take<byte>(hashSize).ToArray<byte>();
48         }
49
50         public Hash(byte[] bytesArray)
51         {
52             _hashBytes = bytesArray;
53         }
54
55         public bool IsZero()
56         {
57             return !_hashBytes.Any(b => b != 0);
58         }
59
60         public override string ToString()
61         {
62             return Interop.ToHex(Interop.ReverseBytes(_hashBytes));
63         }
64     }
65 }