-
Notifications
You must be signed in to change notification settings - Fork 5.3k
Expand file tree
/
Copy pathmain.py
More file actions
62 lines (50 loc) · 1.45 KB
/
main.py
File metadata and controls
62 lines (50 loc) · 1.45 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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import hashlib
import json
from browser import alert, document, html
from browser.local_storage import storage
LOCAL_STORAGE = "hashdata"
def load_data():
data = storage.get(LOCAL_STORAGE)
if data:
hash_map = json.loads(data)
else:
storage[LOCAL_STORAGE] = json.dumps({})
hash_map = {}
return hash_map
def compute(evt):
value = document["text-src"].value
if not value:
alert("You need to enter a value")
return
if value in hash_map:
alert(
f"The SHA-256 value of '{value}' "
f"already exists: '{hash_map[value]}'"
)
return
hash = hashlib.sha256()
hash.update(value.encode())
hash_hex = hash.hexdigest()
hash_map[value] = hash_hex
storage[LOCAL_STORAGE] = json.dumps(hash_map)
display_map()
def clear_map(evt):
hash_map.clear()
storage[LOCAL_STORAGE] = json.dumps({})
document["hash-display"].clear()
def display_map():
if not hash_map:
return
table = html.TABLE(Class="pure-table")
table <= html.THEAD(html.TR(html.TH("Text") + html.TH("SHA-256")))
table <= (
html.TR(html.TD(key) + html.TD(hash_map[key])) for key in hash_map
)
hash_display = document["hash-display"]
hash_display.clear()
hash_display <= table
document["text-src"].value = ""
hash_map = load_data()
display_map()
document["submit"].bind("click", compute)
document["clear-btn"].bind("click", clear_map)