-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhashing.py
46 lines (37 loc) · 1.36 KB
/
hashing.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
import base64
import hashlib as hs
from typing import List
from config import BYTEORDER, ENCOD, SALT_ENCOD
def hash_md5(str_to_hash_: str) -> str:
"""
Hashing a string.
:param str_to_hash_: A string to hash.
:return: A hashed string.
"""
return hs.md5(bytes(str_to_hash_.strip(), encoding=ENCOD)).hexdigest()
def hash_md5_salt(str_to_hash_: str, salt_: str) -> str:
"""
Hashing a string with a secret key.
:param str_to_hash_: A string to hash.
:param salt_: A secret key to hash the string.
:return: A hashed string.
"""
return base64.urlsafe_b64encode(
hs.md5(salt_.encode(SALT_ENCOD) + bytes(int(str_to_hash_.strip()).to_bytes(8, BYTEORDER))).digest()
).decode(SALT_ENCOD)
def choose_hash_method(str_to_hash_: str, salt_: str = None) -> str:
"""
Choose the method of hashing.
:param str_to_hash_: A string to hash.
:param salt_: A secret key to hash the string.
:return: A hashed string.
"""
return hash_md5(str_to_hash_) if salt_ is None else hash_md5_salt(str_to_hash_, salt_)
def hash_lst(lst_: List[int], salt_: str = None) -> List[str]:
"""
Hashing a list of msisdn.
:param lst_: A list of msisdn.
:param salt_: A secret key to hash the msisdn.
:return: A list of hashed msisdn.
"""
return [choose_hash_method(str(msisdn).strip(), salt_) for msisdn in lst_]