-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathBlock.cs
More file actions
134 lines (108 loc) · 4.03 KB
/
Copy pathBlock.cs
File metadata and controls
134 lines (108 loc) · 4.03 KB
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
using System;
using System.Security.Cryptography;
using System.Text;
namespace CSharpToyBitcoinBlockchain
{
class Block
{
public int Index { get; set; }
public string PreviousHash { get; set; }
public string Timestamp { get; set; }
public string Data { get; set; }
public string Hash { get; set; }
int _salt;
public Block(int index, string timestamp, string data, string previousHash = "")
{
this.Index = index;
this.PreviousHash = previousHash;
this.Timestamp = timestamp;
this.Data = data;
this.Hash = this.CalculateHash();
this._salt = 0;
}
// Create the hash of the current block.
public string CalculateHash()
{
var hash = SHA256_hash(this.Index + this.PreviousHash + this.Timestamp + this.Data + this._salt);
return hash;
}
// This is how the mining works!
public void Mine(int difficulty)
{
// You mined successfully if you found a hash with certain number of leading '0's
// difficulty defines the number of '0's required
// e.g. if difficulty is 2, if you found a hash like 00338500000x..., it means you mined it.
while (this.Hash.Substring(0, difficulty) != new String('0', difficulty))
{
this._salt++;
this.Hash = this.CalculateHash();
Console.WriteLine("Mining:" + this.Hash);
}
Console.WriteLine("Block has been mined: " + this.Hash);
}
// Create a hash string from stirng
static string SHA256_hash(string value)
{
var sb = new StringBuilder();
using (SHA256 hash = SHA256Managed.Create())
{
var enc = Encoding.UTF8;
var result = hash.ComputeHash(enc.GetBytes(value));
foreach (var b in result)
sb.Append(b.ToString("x2"));
}
return sb.ToString();
}
}
}
class Block
{
public int Index { get; set; }
public string PreviousHash { get; set; }
public string Timestamp { get; set; }
public string Data { get; set; }
public string Hash { get; set; }
int _salt;
public Block(int index, string timestamp, string data, string previousHash = "")
{
this.Index = index;
this.PreviousHash = previousHash;
this.Timestamp = timestamp;
this.Data = data;
this.Hash = this.CalculateHash();
this._salt = 0;
}
// Create the hash of the current block.
public string CalculateHash()
{
var hash = SHA256_hash(this.Index + this.PreviousHash + this.Timestamp + this.Data + this._salt);
return hash;
}
// This is how the mining works!
public void Mine(int difficulty)
{
// You mined successfully if you found a hash with certain number of leading '0's
// difficulty defines the number of '0's required
// e.g. if difficulty is 2, if you found a hash like 00338500000x..., it means you mined it.
while (this.Hash.Substring(0, difficulty) != new String('0', difficulty))
{
this._salt++;
this.Hash = this.CalculateHash();
Console.WriteLine("Mining:" + this.Hash);
}
Console.WriteLine("Block has been mined: " + this.Hash);
}
// Create a hash string from stirng
static string SHA256_hash(string value)
{
var sb = new StringBuilder();
using (SHA256 hash = SHA256Managed.Create())
{
var enc = Encoding.UTF8;
var result = hash.ComputeHash(enc.GetBytes(value));
foreach (var b in result)
sb.Append(b.ToString("x2"));
}
return sb.ToString();
}
}