-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBlock.cs
53 lines (49 loc) · 1.73 KB
/
Block.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
using Newtonsoft.Json;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Security.Cryptography;
using System.Text;
using System.Text.Json.Serialization;
namespace AlmostBinary_BlockhainLibrary
{
public class Block
{
#region properties
public int Index { get; set; }
public DateTime TimeStamp { get; set; }
public string PreviousHash { get; set; }
public string Hash { get; set; }
public IList<Transaction> Transactions { get; set; }
public int Nonce { get; set; }
#endregion
#region ctor
public Block(DateTime timeStamp, string previousHash, IList<Transaction> transactions)
{
this.Index = 0;
this.TimeStamp = timeStamp;
this.PreviousHash = previousHash;
this.Transactions = transactions;
this.Hash = this.CalculateHash();
}
#endregion
#region methods
public string CalculateHash()
{
SHA512 sha512 = SHA512.Create();
byte[] inputBytes = Encoding.ASCII.GetBytes($"{this.TimeStamp}-{this.PreviousHash ?? ""}-{JsonConvert.SerializeObject(this.Transactions)}-{this.Nonce}");
byte[] outputBytes = sha512.ComputeHash(inputBytes);
return Convert.ToBase64String(outputBytes);
}
public void Mine(int difficulty)
{
var leadingZeros = new string('0', difficulty);
while (this.Hash == null || this.Hash.Substring(0, difficulty) != leadingZeros)
{
this.Nonce++;
this.Hash = this.CalculateHash();
}
}
#endregion
}
}