-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBlock.php
More file actions
44 lines (40 loc) · 1.16 KB
/
Block.php
File metadata and controls
44 lines (40 loc) · 1.16 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
<?php
$key = 'f7457d9234d816aa44f4673e0c5983d4a4a7404e';
class Block {
private $idx;
private $timestamp;
private $data;
private $nonce;
public $previousHash;
public $hash;
public function __construct($idx, $timestamp, $data, $previousHash = '') {
$this->idx = $idx;
$this->timestamp = $timestamp;
$this->data = $data;
$this->previousHash = $previousHash;
$this->nonce = 0;
$this->hash = $this->calculateHash();
}
public function calculateHash() {
$data = [
'idx' => $this->idx,
'previousHash' => $this->previousHash,
'timestamp' => $this->timestamp,
'data' => $this->data,
'nonce' => $this->nonce
];
$dataToString = serialize($data);
$hashed = hash_hmac('sha256', $dataToString, $GLOBALS['key']);
return $hashed;
}
public function mineBlock($difficulty) {
print("Mining block...\n");
while (substr($this->hash, 0, $difficulty) !== str_repeat('0', $difficulty)) {
$this->nonce++;
$this->hash = $this->calculateHash();
}
echo "BLOCK MINED!\n";
print_r($this->hash."\n");
}
}
?>