-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.py
111 lines (92 loc) · 3.25 KB
/
build.py
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
99
100
101
102
103
104
105
106
107
108
109
110
111
import hashlib
import yaml
# Read YAML file
with open("data.yaml", 'r') as stream:
data = yaml.safe_load(stream)
################################ index.html ################################
with open("index_top.html", 'r') as f:
top = f.read()
with open("index_bottom.html", 'r') as f:
bottom = f.read()
middle = ""
for i, enigma in enumerate(data['enigmas']):
middle += f"""
<div class="enigma" id="enigma{i}">
<div class="title">
<h4>Enigma {i+1}:</h4>
<p>{enigma['description']}</p>
</div>
<div class="input">
<label for="password{i}">Answer:</label>
<input type="password{i}" id="password{i}" name="password{i}" />
<button type="button" onclick="checkPassword{i}()">Submit</button>
</div>
<div class="status">
<img src="checked.svg" class="finished" id="enigma{i}-check">
<img src="not_checked.svg" class="not-finished" id="enigma{i}-no-check">
</div>
</div>
"""
full = top + middle + bottom
with open("index.html", 'w') as f:
f.write(full)
################################ script.js ################################
script = ""
for i, enigma in enumerate(data['enigmas']):
m = hashlib.sha256()
clean_answer = str(enigma['answer']).replace(' ', '')
m.update(clean_answer.encode('utf8'))
m.digest()
print(i, enigma['answer'], m.hexdigest())
script += f"""
function checkPassword{i}() {{
var password = document.getElementById("password{i}").value;
var sha_password = SHA256(password.replace(/ /g, ''));
if (
sha_password ==
"{m.hexdigest()}"
) {{
document.getElementById("enigma{i}-check").style = "display: block";
document.getElementById("enigma{i}-no-check").style = "display: none";
}} else {{
document.getElementById("enigma{i}-no-check").style = "display: block";
document.getElementById("enigma{i}-check").style = "display: none";
document.getElementById("password{i}").value = "";
}}
final_code();
}}
checkPassword{i}();
"""
with open("script.js", 'w') as f:
f.write(script)
################################ finish_script.js ################################
sum_answers = ""
for i, enigma in enumerate(data['enigmas']):
clean_answer = str(enigma['answer']).replace(' ', '')
sum_answers += clean_answer
sha_password = hashlib.sha256(sum_answers.encode('utf8')).hexdigest()[:8].upper()
print("Final:", sum_answers, sha_password)
script = """function final_code() {
var password =
"""
for i in range(len(data['enigmas'])-1):
script += f""" document.getElementById("password{i}").value + \n"""
script += f""" document.getElementById("password{len(data['enigmas'])-1}").value;\n"""
script += f"""
var sha_password = SHA256(
password.replace(/ /g, "")
);
var user_code = sha_password.substring(0, 8).toUpperCase();
if (
SHA256(user_code) ==
"{hashlib.sha256(sha_password.encode('utf8')).hexdigest()}"
) {{
document.getElementById("finish-code").innerHTML =
'Finish code: <span id="code">' + user_code + "</span>";
}} else {{
document.getElementById("finish-code").innerHTML = "";
}}
}}
"""
with open("finish_script.js", 'w') as f:
f.write(script)