-
Notifications
You must be signed in to change notification settings - Fork 0
/
hashing.js
51 lines (43 loc) · 996 Bytes
/
hashing.js
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
var crypto = require("crypto");
var poem = [
"The power of a gun can kill",
"and the power of fire can burn",
"the power of wind can chill",
"and the power of a mind can learn",
"the power of anger can rage",
"inside until it tears u apart",
"but the power of a smile",
"especially yours can heal a frozen heart",
];
var Blockchain = {
blocks: [],
};
// Genesis block
Blockchain.blocks.push({
index: 0,
hash: "000000",
data: "",
timestamp: Date.now(),
});
function createBlock(data, index){
const block = {
index,
data,
prevHash: Blockchain.blocks[index-1].hash,
timestamp: Date.now(),
}
block.hash = blockHash(block)
return block;
}
let index = 1;
for (let line of poem) {
Blockchain.blocks.push(createBlock(line, index))
index++;
}
function blockHash(bl) {
return crypto.createHash("sha256").update(
// use block data to calculate hash
JSON.stringify(bl)
).digest("hex");
}
console.log(Blockchain.blocks)