Skip to content

Commit 71d51e4

Browse files
committed
Added SHA256 example
Structure for Vue.js
1 parent 063b72d commit 71d51e4

File tree

4 files changed

+187
-0
lines changed

4 files changed

+187
-0
lines changed

brython/sha256/index.html

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<!DOCTYPE html >
2+
<html>
3+
<head>
4+
<meta charset="utf-8"/>
5+
<link
6+
rel="stylesheet"
7+
href="https://cdnjs.cloudflare.com/ajax/libs/pure/2.0.3/pure-min.min.css"
8+
integrity="sha256-jYujp4Kf07YDuUF9i1MHo4AnpXUKuHxIUXH7CrHxdKw="
9+
crossorigin="anonymous"
10+
async />
11+
<script
12+
src="https://cdnjs.cloudflare.com/ajax/libs/brython/3.8.9/brython.min.js"
13+
integrity="sha256-U56d9Sn/Gtf1meSBXazW81LM1bUeyc1jFuoY3CBu6A8="
14+
crossorigin="anonymous"
15+
async>
16+
</script>
17+
<script
18+
src="https://cdnjs.cloudflare.com/ajax/libs/brython/3.8.9/brython_stdlib.min.js"
19+
integrity="sha256-twMHOlRBOpBkpyGFeXIBWoJqTpqf76Zp8HExOelwyRc="
20+
crossorigin="anonymous"
21+
async>
22+
</script>
23+
<script src="main.py" type="text/python" defer></script>
24+
<style>
25+
body { padding: 30px; }
26+
</style>
27+
</head>
28+
<body onload="brython()">
29+
<form class="pure-form" onsubmit="return false;">
30+
<fieldset>
31+
<legend>SHA-256 Calculator</legend>
32+
<input type="text" id="text-src" placeholder="Text to Encode" autocomplete="off"/>
33+
<button type="submit" id="submit" class="pure-button pure-button-primary">Ok</button>
34+
<button id="clear-btn" class="pure-button">Clear</button>
35+
</fieldset>
36+
</form>
37+
<div id="hash-display"></div>
38+
</body>
39+
</html>

brython/sha256/main.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
from browser import document, prompt, html, alert
2+
from browser.local_storage import storage
3+
import json, hashlib
4+
5+
LOCAL_STORAGE = 'hashdata'
6+
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
15+
16+
def compute(evt):
17+
value = document["text-src"].value
18+
if not value:
19+
alert("You need to enter a value")
20+
return
21+
if value in hash_map:
22+
alert(f"The SHA-256 value of '{value}' already exists: '{hash_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+
52+

brython/vuejs/index.html

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<!DOCTYPE html >
2+
<html>
3+
<head>
4+
<meta charset="utf-8"/>
5+
<link
6+
rel="stylesheet"
7+
href="https://cdnjs.cloudflare.com/ajax/libs/pure/2.0.3/pure-min.min.css"
8+
integrity="sha256-jYujp4Kf07YDuUF9i1MHo4AnpXUKuHxIUXH7CrHxdKw="
9+
crossorigin="anonymous"
10+
async/>
11+
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.6.11/vue.min.js"
12+
integrity="sha256-ngFW3UnAN0Tnm76mDuu7uUtYEcG3G5H1+zioJw3t+68="
13+
crossorigin="anonymous"
14+
async>
15+
</script>
16+
<script
17+
src="https://cdnjs.cloudflare.com/ajax/libs/brython/3.8.9/brython.min.js"
18+
integrity="sha256-U56d9Sn/Gtf1meSBXazW81LM1bUeyc1jFuoY3CBu6A8="
19+
crossorigin="anonymous"
20+
defer>
21+
</script>
22+
<script
23+
src="https://cdnjs.cloudflare.com/ajax/libs/brython/3.8.9/brython_stdlib.min.js"
24+
integrity="sha256-twMHOlRBOpBkpyGFeXIBWoJqTpqf76Zp8HExOelwyRc="
25+
crossorigin="anonymous"
26+
async>
27+
</script>
28+
<script src="main.py" type="text/python" defer></script>
29+
<style>
30+
body { padding: 30px; }
31+
</style>
32+
</head>
33+
<body onload="brython()">
34+
<form class="pure-form" onsubmit="return false;">
35+
<fieldset>
36+
<legend>SHA-256 Calculator</legend>
37+
<input type="text" id="text-src" placeholder="Text to Encode" autocomplete="off"/>
38+
<button type="submit" id="submit" class="pure-button pure-button-primary">Ok</button>
39+
<button id="clear-btn" class="pure-button">Clear</button>
40+
</fieldset>
41+
</form>
42+
<div id="hash-display"></div>
43+
</body>
44+
</html>

brython/vuejs/main.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
from browser import document, prompt, html, alert
2+
from browser.local_storage import storage
3+
import json, hashlib
4+
5+
LOCAL_STORAGE = 'hashdata'
6+
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
15+
16+
def compute(evt):
17+
value = document["text-src"].value
18+
if not value:
19+
alert("You need to enter a value")
20+
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+
52+

0 commit comments

Comments
 (0)