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