-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.py
60 lines (48 loc) · 1.62 KB
/
util.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import hashlib, random, config, re, bleach
from cryptography.fernet import Fernet
from usernames import is_safe_username
def isAllowed(text, max_len=10, min_len=3):
if len(text) <=max_len:
if len(text) < min_len:
return False
if text.find(".") > -1:
return False
return is_safe_username(text)
def _get_key(path):
try:
f = open(path, "rb")
key = f.read()
f.close()
return key
except:
key = Fernet.generate_key()
f = open(path, "wb")
f.write(key)
f.close()
return key
def generate_uid(string: str, hash=hashlib.sha3_512) -> str:
string = f"^%$#!#$^&*({string})$&^hjvghfttf_".encode()
return hash(string).hexdigest() + "==hash"
def generate_id() -> str:
string = "abcdefghijklmnopqrstuvwxyz1234567890!@#$%^&**(())(*&^$$$"
upper_string = string.upper()
string = string + upper_string
l=[]
b=[]
for x in string:
l.append(x)
random.shuffle(l)
h = hashlib.sha512("".join(l).encode())
b.append(h.hexdigest())
return hashlib.sha3_512("".join(l).encode()).hexdigest() + '==id'
class FCipher:
def __init__(self, path_to_key):
self._data = Fernet(_get_key(path_to_key))
def encrypt(self, data):
return self._data.encrypt(data)
def decrypt(self, data):
return self._data.decrypt(data)
def filter(text):
return bleach.clean(text, tags=config.allowed_tags, attributes=config.allowed_attrs, strip=False)
if __name__=="__main__":
print(isAllowed(username))