-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBlock.cs
60 lines (56 loc) · 1.79 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
54
55
56
57
58
59
60
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;
namespace Blockchain
{
[Serializable]
public struct Block
{
[JsonProperty("index")]
public int index;
[JsonProperty("hash")]
public string hash;
[JsonProperty("prev_hash")]
public string prev_hash;
[JsonProperty("timestamp")]
public DateTime timestamp;
[JsonProperty("data")]
public string data;
[JsonProperty("salt")]
public string salt;
public override string ToString()
{
return $"Block<p={prev_hash}>, h={hash}>";
}
public string GetHash()
{
string hash = "";
using (MemoryStream ms = new MemoryStream())
{
do
{
ms.Flush();
ms.Seek(0, SeekOrigin.Begin);
salt = BlockManager.RandomString(10);
byte[] bytes;
bytes = BitConverter.GetBytes(index);
ms.Write(bytes, 0, bytes.Length);
bytes = Encoding.UTF8.GetBytes(salt + hash);
ms.Write(bytes, 0, bytes.Length);
bytes = Encoding.UTF8.GetBytes(salt + prev_hash);
ms.Write(bytes, 0, bytes.Length);
bytes = BitConverter.GetBytes(timestamp.Ticks);
ms.Write(bytes, 0, bytes.Length);
bytes = Encoding.UTF8.GetBytes(salt + data);
ms.Write(bytes, 0, bytes.Length);
hash = Hash.GetHash(bytes);
}
while(!hash.Contains("b0330b"));
}
return hash;
}
}
}