-
Notifications
You must be signed in to change notification settings - Fork 132
/
api.py
46 lines (28 loc) · 1.22 KB
/
api.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
from .algorithms import DECODING_ALGORITHMS, ENCODING_ALGORITHMS, HASHING_ALGORITHMS
def encoding_algos() -> list[str]:
return list(ENCODING_ALGORITHMS.keys())
def decoding_algos() -> list[str]:
return list(DECODING_ALGORITHMS.keys())
def hashing_algos() -> list[str]:
return list(HASHING_ALGORITHMS.keys())
def has_encoding_algo(algo: str) -> bool:
return algo.lower().strip() in ENCODING_ALGORITHMS
def has_decoding_algo(algo: str) -> bool:
return algo.lower().strip() in DECODING_ALGORITHMS
def has_hashing_algo(algo: str) -> bool:
return algo.lower().strip() in HASHING_ALGORITHMS
def encode(text: str | bytes, algo: str) -> str:
if isinstance(text, str):
text = text.encode()
encoding_fn = ENCODING_ALGORITHMS[algo.lower().strip()]
return encoding_fn(text).decode()
def decode(text: str | bytes, algo: str) -> str:
if isinstance(text, str):
text = text.encode()
decoding_fn = DECODING_ALGORITHMS[algo.lower().strip()]
return decoding_fn(text).decode()
def hash_val(text: str | bytes, algo: str) -> str:
if isinstance(text, str):
text = text.encode()
hashing_fn = HASHING_ALGORITHMS[algo.lower().strip()]
return hashing_fn(text).hexdigest()