|
1 | | -from browser import document, prompt, html, alert |
2 | | -from browser.local_storage import storage |
| 1 | +from browser import alert, document, window |
3 | 2 | import json, hashlib |
| 3 | +from javascript import this |
4 | 4 |
|
5 | | -LOCAL_STORAGE = 'hashdata' |
| 5 | +hashes = { |
| 6 | + "sha-1": hashlib.sha1, |
| 7 | + "sha-256": hashlib.sha256, |
| 8 | + "sha-512": hashlib.sha512 |
| 9 | +} |
6 | 10 |
|
7 | | -def load_data(): |
8 | | - data = storage.get(LOCAL_STORAGE) |
9 | | - if data: |
10 | | - hash_map = json.loads(data) |
11 | | - else: |
12 | | - storage[LOCAL_STORAGE] = json.dumps({}) |
13 | | - hash_map = {} |
14 | | - return hash_map |
| 11 | +Vue = window.Vue |
15 | 12 |
|
16 | | -def compute(evt): |
17 | | - value = document["text-src"].value |
| 13 | +def compute_hash(evt): |
| 14 | + value = this().input_text |
18 | 15 | if not value: |
19 | 16 | alert("You need to enter a value") |
20 | 17 | return |
21 | | - if value in hash_map: |
22 | | - alert(f"The SHA-256 value of '{value}' already exists: '{sha_map[value]}'") |
23 | | - return |
24 | | - hash = hashlib.sha256() |
25 | | - hash.update(value.encode()) |
26 | | - hash_hex = hash.hexdigest() |
27 | | - hash_map[value] = hash_hex |
28 | | - storage[LOCAL_STORAGE] = json.dumps(hash_map) |
29 | | - display_map() |
30 | | - |
31 | | -def clear_map(evt): |
32 | | - hash_map.clear() |
33 | | - storage[LOCAL_STORAGE] = json.dumps({}) |
34 | | - document["hash-display"].clear() |
35 | | - |
36 | | -def display_map(): |
37 | | - if not hash_map: |
38 | | - return |
39 | | - table = html.TABLE(Class="pure-table") |
40 | | - table <= html.THEAD(html.TR(html.TH("Text") + html.TH("SHA-256"))) |
41 | | - table <= (html.TR(html.TD(key) + html.TD(hash_map[key])) for key in hash_map) |
42 | | - hash_display = document["hash-display"] |
43 | | - hash_display.clear() |
44 | | - hash_display <= table |
45 | | - document["text-src"].value = "" |
46 | | - |
47 | | -hash_map = load_data() |
48 | | -display_map() |
49 | | -document["submit"].bind("click", compute) |
50 | | -document["clear-btn"].bind("click", clear_map) |
51 | | - |
| 18 | + hash_object = hashes[this().algo]() |
| 19 | + hash_object.update(value.encode()) |
| 20 | + hex_value = hash_object.hexdigest() |
| 21 | + this().hash_value = hex_value |
| 22 | + |
| 23 | +app = Vue.new({ |
| 24 | + "el": "#app", |
| 25 | + "data": { |
| 26 | + "hash_value": "", |
| 27 | + "algos": [ "sha-1", "sha-256", "sha-512" ], |
| 28 | + "algo": "sha-1", |
| 29 | + "input_text": "" |
| 30 | + }, |
| 31 | + "methods": { |
| 32 | + "compute_hash": compute_hash |
| 33 | + } |
| 34 | +}) |
52 | 35 |
|
0 commit comments