forked from drewstaylor/hashchains
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
88 lines (71 loc) · 2.22 KB
/
index.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
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
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jshashes/1.0.6/hashes.min.js"></script>
<script>
// generate seed
function seed(num_zero_nibbles=32, num_random_nibbles=32) {
var s = "";
// first add zeros
for (var i = 0; i < num_zero_nibbles; i++) { s += "0"; }
// then add randoms
var allowed = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
for (var i = 0; i < num_random_nibbles; i++) {
s += allowed.charAt(Math.floor(Math.random() * allowed.length));
}
return s;
}
// hashchain
function hashchain(seed, num_links) {
var h = seed
for (var i = 0; i < num_links; i++) {
h = new Hashes.SHA256().hex(h)
}
return h
}
function prove(age) {
P = hashchain(S, Number(age)+1)
document.getElementById("display_P").innerHTML = P
return P
}
function challenge(age, age_to_prove) {
Q = hashchain(S, Number(age)+1-Number(age_to_prove))
document.getElementById("display_Q").innerHTML = Q
return Q
}
function verify(Q, age_to_prove, P) {
V = hashchain(Q, Number(age_to_prove))
document.getElementById("display_V").innerHTML = V
if (P==V) {
document.getElementById("verified").innerHTML = "VERIFIED!"
}
else {
document.getElementById("verified").innerHTML = "FAILED!"
}
return V
}
</script>
<button onclick='S=seed();document.getElementById("display_S").innerHTML = S'>
recreate seed
</button>
<hr/>
<p id="display_S"></p>
<p>Actual Age: <input type="text" name="actual_age" id="actual_age" value="38">
<button onclick='P=prove(document.getElementById("actual_age").value)'>
create Proof
</button>
<div id="display_P"></div>
<p>Age To Prove: <input type="text" name="age_to_prove" id="age_to_prove" value="19">
<button onclick='Q=challenge(document.getElementById("actual_age").value, document.getElementById("age_to_prove").value)'>
create Challenge
</button>
<div id="display_Q"></div>
<hr/>
<button onclick='V=verify(Q, document.getElementById("age_to_prove").value, P)'>
verify challenge
</button>
<br/><br/>
<div id="display_V"></div>
<div id="verified"></div>
<script>
// start with a seed
S=seed();
document.getElementById("display_S").innerHTML = S
</script>