-
Notifications
You must be signed in to change notification settings - Fork 1
/
Block.cs
49 lines (44 loc) · 1.42 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
using System;
using System.Security.Cryptography;
using System.Text;
using System.Linq;
namespace BlockChainTutorial1
{
public class Block
{
public int index;
public DateTime timestamp;
public object data = new object();
public string previousHash;
public string hash;
public int nonce;
public Block(int _index, DateTime _timestamp, object _data, string _previousHash="")
{
index = _index;
timestamp = _timestamp;
data = _data;
previousHash = _previousHash;
hash = calculateHash();
nonce = 0;
}
public string calculateHash(){
using(var sha256 = SHA256.Create())
{
var hashedBytes = sha256.ComputeHash(
Encoding.UTF8.GetBytes(index + previousHash + timestamp + Newtonsoft.Json.JsonConvert.SerializeObject(data) + this.nonce)
);
return BitConverter.ToString(hashedBytes).Replace("-", "").ToLower();
}
}
public void mineBlock(int difficulty)
{
String s = new String(new char[difficulty]);
s = s.Replace("\0", "0");
while (hash.Substring(0,difficulty)!=s){
this.nonce++;
this.hash = this.calculateHash();
}
Console.Write("Block Mining"+hash);
}
}
}