dp-prac 0.1.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
dp-prac/__init__.py ADDED
File without changes
dp-prac/__main__.py ADDED
@@ -0,0 +1,5 @@
1
+ from dp_prac.__main__ import main
2
+
3
+
4
+ if __name__ == "__main__":
5
+ main()
dp_prac/__init__.py ADDED
File without changes
dp_prac/__main__.py ADDED
@@ -0,0 +1,61 @@
1
+ from importlib.resources import files
2
+ from pathlib import Path
3
+ import sys
4
+
5
+
6
+ PRACTICALS = (
7
+ "Caesar cipher",
8
+ "Rail Fence cipher",
9
+ "SHA-256 password hash",
10
+ "Check for leaked passwords",
11
+ "Generate a passphrase",
12
+ "Exhaustive search puzzle",
13
+ "Sign and verify a document",
14
+ )
15
+
16
+ SAMPLE_FILES = {"4": "passwords.csv", "5": "words.txt"}
17
+
18
+
19
+ def export_file(source, destination):
20
+ try:
21
+ content = source.read_bytes()
22
+ with destination.open("xb") as output:
23
+ output.write(content)
24
+ except FileExistsError:
25
+ print(f"{destination.name} already exists.")
26
+ except OSError as error:
27
+ print(f"Could not create {destination.name}: {error}", file=sys.stderr)
28
+ else:
29
+ print(f"Created {destination}")
30
+
31
+
32
+ def main():
33
+ directory = Path.cwd()
34
+ solutions = files("dp_prac").joinpath("solutions")
35
+ choices = {str(number): title for number, title in enumerate(PRACTICALS, 1)}
36
+ print("DP Practicals")
37
+ print(f"Output folder: {directory}\n")
38
+ for number, title in choices.items():
39
+ print(f"{number}. {title}")
40
+ print("0. Exit")
41
+
42
+ while True:
43
+ try:
44
+ choice = input("\nChoose a question (1-7), or 0 to exit: ").strip()
45
+ except (EOFError, KeyboardInterrupt):
46
+ print()
47
+ return
48
+ if choice == "0":
49
+ return
50
+ if choice not in choices:
51
+ print("Enter a number from 1 to 7, or 0 to exit.")
52
+ continue
53
+
54
+ export_file(solutions.joinpath(f"q{choice}.txt"), directory / f"q{choice}.py")
55
+ if choice in SAMPLE_FILES:
56
+ sample = SAMPLE_FILES[choice]
57
+ export_file(solutions.joinpath(sample), directory / sample)
58
+
59
+
60
+ if __name__ == "__main__":
61
+ main()
@@ -0,0 +1 @@
1
+ student1,password
@@ -0,0 +1,20 @@
1
+ def shift_letters(text, shift):
2
+ result = ""
3
+ for letter in text:
4
+ if "A" <= letter <= "Z":
5
+ alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
6
+ elif "a" <= letter <= "z":
7
+ alphabet = "abcdefghijklmnopqrstuvwxyz"
8
+ else:
9
+ result += letter
10
+ continue
11
+ result += alphabet[(alphabet.index(letter) + shift) % 26]
12
+ return result
13
+
14
+
15
+
16
+ text = input("Enter text (example: Hello!): ")
17
+ shift = int(input("Enter a shift (example: 3): "))
18
+ encrypted = shift_letters(text, shift)
19
+ print("Encrypted:", encrypted)
20
+ print("Decrypted:", shift_letters(encrypted, -shift))
@@ -0,0 +1,31 @@
1
+ text = input("Enter text (example: HELLOWORLD): ")
2
+ rails = int(input("Enter the number of rails (example: 3): "))
3
+ path = []
4
+ rail = 0
5
+ step = 1
6
+ for letter in text:
7
+ path.append(rail)
8
+ if rail == 0:
9
+ step = 1
10
+ elif rail == rails - 1:
11
+ step = -1
12
+ rail += step
13
+
14
+
15
+ positions = []
16
+ for rail in range(rails):
17
+ for index in range(len(text)):
18
+ if path[index] == rail:
19
+ positions.append(index)
20
+
21
+
22
+ encrypted = ""
23
+ for index in positions:
24
+ encrypted += text[index]
25
+ print("Encrypted:", encrypted)
26
+
27
+
28
+ decrypted = [""] * len(text)
29
+ for index, letter in zip(positions, encrypted):
30
+ decrypted[index] = letter
31
+ print("Decrypted:", "".join(decrypted))
@@ -0,0 +1,10 @@
1
+ from hashlib import sha256
2
+
3
+
4
+
5
+ def hash_password(password):
6
+ return sha256(password.encode()).hexdigest()
7
+
8
+
9
+
10
+ print("SHA-256:", hash_password(input("Demo password (example: abc): ")))
@@ -0,0 +1,21 @@
1
+ from csv import reader
2
+ from hashlib import sha1
3
+ from urllib.request import Request, urlopen
4
+
5
+
6
+ with open("passwords.csv", encoding="utf-8", newline="") as file:
7
+ for username, password in reader(file):
8
+ digest = sha1(password.encode()).hexdigest().upper()
9
+ request = Request(
10
+ "https://api.pwnedpasswords.com/range/" + digest[:5],
11
+ headers={"Add-Padding": "true"},
12
+ )
13
+ with urlopen(request, timeout=10) as response:
14
+ lines = response.read().decode().splitlines()
15
+ status = "No match found"
16
+ for line in lines:
17
+ suffix, count = line.split(":")
18
+ if suffix == digest[5:] and int(count) > 0:
19
+ status = "Leaked"
20
+ break
21
+ print(username + ":", status)
@@ -0,0 +1,9 @@
1
+ from pathlib import Path
2
+ from secrets import choice
3
+
4
+
5
+ words = Path("words.txt").read_text(encoding="utf-8").split()
6
+ picked = []
7
+ for _ in range(4):
8
+ picked.append(choice(words))
9
+ print("Passphrase:", "-".join(picked))
@@ -0,0 +1,9 @@
1
+ from itertools import product
2
+
3
+
4
+ for first, second in product("ABC", repeat=2):
5
+ pair = first + second
6
+ print(pair)
7
+ if pair == "BC":
8
+ print("Puzzle solved")
9
+ break
@@ -0,0 +1,20 @@
1
+ from pathlib import Path
2
+ from cryptography.hazmat.primitives.asymmetric.ed25519 import (
3
+ Ed25519PrivateKey, Ed25519PublicKey,
4
+ )
5
+
6
+
7
+ # Sender
8
+ document = Path("notice.txt")
9
+ document.write_text("Practical starts at 10 AM.", encoding="utf-8")
10
+ private_key = Ed25519PrivateKey.generate()
11
+ Path("notice.sig").write_bytes(private_key.sign(document.read_bytes()))
12
+ Path("public.key").write_bytes(private_key.public_key().public_bytes_raw())
13
+
14
+
15
+ # Receiver
16
+ public_key = Ed25519PublicKey.from_public_bytes(
17
+ Path("public.key").read_bytes()
18
+ )
19
+ public_key.verify(Path("notice.sig").read_bytes(), document.read_bytes())
20
+ print("Signature verified.")
@@ -0,0 +1,4 @@
1
+ river
2
+ mango
3
+ cloud
4
+ stone
@@ -0,0 +1,22 @@
1
+ Metadata-Version: 2.4
2
+ Name: dp-prac
3
+ Version: 0.1.0
4
+ Summary: Export seven beginner Data Privacy practicals as Python scripts
5
+ Requires-Python: >=3.10
6
+ Description-Content-Type: text/markdown
7
+ Provides-Extra: run
8
+ Requires-Dist: cryptography>=40; extra == "run"
9
+
10
+ # DP Practicals
11
+
12
+ Install with `py -m pip install dp-prac` and run `dp-prac`.
13
+ On Windows you can also launch it with `py -m dp-prac`.
14
+
15
+ Choose a number from 1 to 7 to export the corresponding script, such as
16
+ `q1.py`, into the current folder. Existing files are preserved. Run an
17
+ exported script from that folder with a command such as `py q1.py`.
18
+
19
+ Q4 includes a demo `passwords.csv` and needs internet for the Pwned Passwords
20
+ check. Q5 includes `words.txt`. Q7 needs `cryptography`, which you can install
21
+ with `py -m pip install "dp-prac[run]"`. The menu itself has no runtime
22
+ dependencies.
@@ -0,0 +1,18 @@
1
+ dp-prac/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
2
+ dp-prac/__main__.py,sha256=UbT8EWYUb4DucIy6cNFEttP-H8w4Xf5Rf9xzfe5P-aw,75
3
+ dp_prac/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
4
+ dp_prac/__main__.py,sha256=bpseo-HadQp3UUgo65i3G3dhWXU5DPViPTWgCi8rDwM,1744
5
+ dp_prac/solutions/passwords.csv,sha256=UQgJus9Ne78a9DxVSIP9HYdf6BYunKBsf7wwdFATgOo,19
6
+ dp_prac/solutions/q1.txt,sha256=NtcEmhtM55exwH5cVKO29zBZOpnhCwg824lVW2TJ3s8,637
7
+ dp_prac/solutions/q2.txt,sha256=daBedepMFRiPJuPn4sfe---TdM7pICh8Ph1I5GsHlis,709
8
+ dp_prac/solutions/q3.txt,sha256=0Mkza8TGgA09Pq-SVoLMORX88ZyctFH6F_lCIQYVf0c,195
9
+ dp_prac/solutions/q4.txt,sha256=6nH4scnvpoJOrsL2-_QzzBrG3JRKLtiofpMpnF6k-lc,802
10
+ dp_prac/solutions/q5.txt,sha256=_RMvCtSljwxzSKg7ETihlTCwUHg-qhdAV6DEkgoikMg,228
11
+ dp_prac/solutions/q6.txt,sha256=CSwD7oT1pfTSEDvP7j-CX4GXZM-CKVJXlsenyPjXT7A,196
12
+ dp_prac/solutions/q7.txt,sha256=g3PP6dhG2IJff1agXRyrlzOQzq_-lpuU7s5-eq7alD4,655
13
+ dp_prac/solutions/words.txt,sha256=Gtegmchcg5q3YW7QuEf5YWTTjhfZnSumrF5eCpL77wM,28
14
+ dp_prac-0.1.0.dist-info/METADATA,sha256=kEOmh6aBs9A1Y7UZRPs2vc3dd3SuZ-RqgU_mQU0YdXk,868
15
+ dp_prac-0.1.0.dist-info/WHEEL,sha256=mOdXR9C3wMVov7cSY8PY-XCzo3SmmWCHvGQr5csJoao,91
16
+ dp_prac-0.1.0.dist-info/entry_points.txt,sha256=tVeEx_Ck2N6wNdFq6xpqeDVZ9mVYknMQKnq4qiPVCTk,50
17
+ dp_prac-0.1.0.dist-info/top_level.txt,sha256=JXpAXmqPlonVrBIqQT37l7U6vcKKPOXNuPXSuNrrYbk,16
18
+ dp_prac-0.1.0.dist-info/RECORD,,
@@ -0,0 +1,5 @@
1
+ Wheel-Version: 1.0
2
+ Generator: setuptools (84.2.1)
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any
5
+
@@ -0,0 +1,2 @@
1
+ [console_scripts]
2
+ dp-prac = dp_prac.__main__:main
@@ -0,0 +1,2 @@
1
+ dp-prac
2
+ dp_prac