前端页面计算SHA256(模拟区块链货币挖矿过程)

前端页面计算SHA256

模拟计算区块链货币的“挖矿”过程。参考课程:https://www.icourse163.org/learn/NJU-1449346161

使用了CryptoJS计算SHA256,使用bootstrap框架。

HTML

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
<body onload="onLoad()">
<div class="container">
<div class="line">
<div class="label">区块: </div>
<div class="input-group">
<div class="input-group-prepend">
<span class="input-group-text" id="basic-addon1">#</span>
</div>
<input id="block" type="number" class="form-control" placeholder="block number" aria-label="block" value="1" onchange="calc()">
</div>
</div>
<div class="line">
<div class="label">随机数: </div>
<div class="input-group">
<input id="random" type="number" class="form-control" placeholder="random number" aria-label="random" value="0" onchange="calc()">
</div>
</div>
<div class="line" style="align-items: flex-start;">
<div class="label">数据: </div>
<textarea id="data" class="form-control" aria-label="With textarea" style="min-height: 150px;" onchange="calc()"></textarea>
</div>
<div class="line">
<div class="label">SHA256: </div>
<div class="input-group">
<textarea id="hash" type="text" class="form-control" placeholder="hash number" aria-label="hash" disabled="true" style="word-break:break-all;word-wrap:break-all;resize:unset;"></textarea>
</div>
</div>
<div class="line">
<div class="label">前几位为0?</div>
<div class="input-group">
<input id="int" type="number" class="form-control" placeholder="int" aria-label="int" value="3">
</div>
</div>
<div class="btn-area">
<button id="btn" onclick="start()" type="button" class="btn btn-success">Start!</button>
</div>
</div>
</body>

CSS

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
.container {
height: auto;
min-width: 420px;
background-color: rgba(226, 240, 220, 200);
margin-top: 4rem;
margin-bottom: 4rem;
border-radius: 5px;
display: flex;
flex-direction: column;
justify-content: flex-start;
align-items: center;
}

.line {
width: 95%;
display: flex;
flex-direction: row;
justify-content: center;
align-items: center;
margin-top: 15px;
}

.label {
width: 100px;
margin-right: 10px;
text-align: right;
}

.btn-area {
width: 90%;
display: flex;
flex-direction: row;
justify-content: left;
align-items: center;
margin-top: 15px;
margin-bottom: 20px;
}

JS

格式化的字符串为:block=${block};random=${random};data=${data}

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
var block = 1;
var random = 0;
var data = "Hello World";
var int = 3;
var hash = "NULL";

function repeat(src, n) {
return (new Array(n + 1)).join(src);
}

function writeLocal() {
json = `{"block": "${block}", "random": "${random}", "data": "${data}", "int": "${int}", "hash": "${hash}"}`;
localStorage.calc_hash = json;
document.getElementById('block').value = block;
document.getElementById('random').value = random;
document.getElementById('data').value = data;
document.getElementById('int').value = int;
document.getElementById('hash').value = hash;
}

function readLocal() {
json = localStorage.calc_hash;
if (json != null) {
json = JSON.parse(json);
} else {
json = null;
}
console.log(json);
return json;
}

function calc() {
block = document.getElementById('block').value;
random = document.getElementById('random').value;
data = document.getElementById('data').value;
var input = `block=${block};random=${random};data=${data}`;
var output = CryptoJS.SHA256(input);
hash = output.toString(CryptoJS.enc.Hex).toUpperCase();
document.getElementById('block').value = block;
document.getElementById('random').value = random;
document.getElementById('data').value = data;
document.getElementById('hash').value = hash;
}

function start() {
block = document.getElementById('block').value;
random = document.getElementById('random').value;
data = document.getElementById('data').value;
int = Number(document.getElementById('int').value);
if (int < 2) {
alert("取0的个数太少了!");
int = 2;
document.getElementById('int').value = int;
return;
} else if (int > 9) {
alert("取0的个数太多了!\n你想让你的电脑燃烧吗??");
int = 4;
document.getElementById('int').value = int;
return;
}
random = 0;
var now, target, input, output;
target = repeat('0', int);
document.getElementById('random').value = '0';
document.getElementById('hash').value = '计算中...';
setTimeout(() => {
while (now != target) {
input = `block=${block};random=${random};data=${data}`;
output = CryptoJS.SHA256(input);
hash = output.toString(CryptoJS.enc.Hex);
now = hash.slice(0, int);
console.log(now);
random += 1;
}
random -= 1;
hash = hash.toUpperCase();
writeLocal();
}, 500);
}

function onLoad() {
localData = readLocal();
if (localData != null) {
block = localData.block;
random = localData.random;
data = localData.data;
int = localData.int;
hash = localData.hash;
document.getElementById('block').value = block;
document.getElementById('random').value = random;
document.getElementById('data').value = data;
document.getElementById('int').value = int;
document.getElementById('hash').value = hash;
} else {
writeLocal();
calc();
}
}

CodePen 预览

https://codepen.io/irispro/full/jOrKodY

打赏
  • 版权声明: 本博客采用 Apache License 2.0 许可协议。
    转载请注明出处: https://ryzenx.com/2020/11/Front_Calc_SHA265/

谢谢你的喜欢~

支付宝
微信