Implement some CScript functionality
[NovacoinLibrary.git] / Novacoin / Hash.cs
1 \feffusing System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using System.Threading.Tasks;
6
7 namespace Novacoin
8 {
9     public abstract class Hash
10     {
11         /// <summary>
12         /// Array of digest bytes.
13         /// </summary>
14         private byte[] _hashBytes = null;
15
16         /// <summary>
17         /// Hash size, must be overriden
18         /// </summary>
19         public virtual int hashSize 
20         {
21             get; 
22             private set;
23         }
24
25         public IList<byte> hashBytes
26         {
27             get { return new List<byte>(_hashBytes); }
28         }
29
30         /// <summary>
31         /// Initializes an empty instance of the Hash class.
32         /// </summary>
33         public Hash()
34         {
35             _hashBytes = Enumerable.Repeat<byte>(0, hashSize).ToArray();
36         }
37
38         /// <summary>
39         /// Initializes a new instance of Hash class with first 20 bytes from supplied list
40         /// </summary>
41         /// <param name="bytesList">List of bytes</param>
42         public Hash(IList<byte> bytesList)
43         {
44             _hashBytes = bytesList.Take<byte>(hashSize).ToArray<byte>();
45         }
46
47         public Hash(byte[] bytesArray)
48         {
49             _hashBytes = bytesArray;
50         }
51
52         public override string ToString()
53         {
54             StringBuilder sb = new StringBuilder(hashSize * 2);
55             foreach (byte b in _hashBytes)
56             {
57                 sb.AppendFormat("{0:x2}", b);
58             }
59             return sb.ToString();
60         }
61     }
62 }