This repository was archived by the owner on Apr 7, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUtility.py
More file actions
82 lines (64 loc) · 2.32 KB
/
Utility.py
File metadata and controls
82 lines (64 loc) · 2.32 KB
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
from random import randint
import json
import os
import hashlib
import re
words = None
def GetRandomWords(app, passageLength: int):
global words
if words == None:
jsonFile = app.open_resource("Words.json")
words = json.loads(jsonFile.read())
jsonFile.close()
passage: str = ""
for x in range(0, passageLength):
randomWord = words[randint(0, len(words) - 1)] + " "
passage += randomWord
passage = passage[:-1]
return passage
def HashPassword(password: str, saltUsed: bytes = None):
if saltUsed == None:
saltUsed = os.urandom(32)
hashedPassword = hashlib.pbkdf2_hmac(
'sha256', # The hash digest algorithm for HMAC
password.encode('utf-8'), # Convert the password to bytes
saltUsed,
100000 # It is recommended to use at least 100,000 iterations of SHA-256
)
return hashedPassword, saltUsed
def ValidateUserData(username: str, email: str, password: str):
# Presence check for all variables
if username == None or username == "":
return "Please enter a username!"
elif email == None or email == "":
return "Please enter an email!"
elif password == None or password == "":
return "Please enter a password!"
# Validate username.
for x in username:
if not x.isalpha() and not x.isnumeric():
return "Username should only contain alphabets or numbers!"
if len(username) > 50: # Length Check
return "Username should not be longer than 50 characters!"
# Validate email
if not re.search('^[a-z0-9]+[\._]?[a-z0-9]+[@]\w+[.]\w{2,3}$', email):
return "Please enter a valid email!"
# Validate password. Rules: length > 8. Contains both numbers and alphabets
if len(password) < 8:
return "Password must be at least 8 characters long!"
containsAlpha = containsNum = False
for x in password:
if x.isalpha():
containsAlpha = True
if x.isnumeric():
containsNum = True
if containsAlpha == False or containsNum == False:
return "Password must contain both an alphabet and a number"
return ""
def clamp(value: int, minValue: int, maxValue: int):
if value >= maxValue:
return maxValue
elif value <= minValue:
return minValue
else:
return value