-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhasher.py
75 lines (57 loc) · 1.76 KB
/
hasher.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
'''
Hasher.py
This module is a helper file made to hash and encrypt passwords and user data
It should not be run by the user
'''
from hashlib import pbkdf2_hmac as hash_algo
from os import urandom
import sys
import base64
from cryptography.fernet import Fernet
SALT_FILE = 'shadow_salt.txt'
NO_SALT_FILE = 'shadow_no_salt.txt'
def make_hash(password):
salt = urandom(1)
key = hash_algo('sha256', password.encode('utf-8'), salt, 100000)
return salt, key
def calc_hash(salt, password):
return hash_algo('sha256', password.encode('utf-8'), salt, 100000)
def register_account(usr_name, usr_pass):
salt, hash = make_hash(usr_pass)
return f'{usr_name}:{salt}${hash}'
def log_new_user_salted(username, user_password):
salt, hash = make_hash(user_password)
with open(SALT_FILE, 'a') as fOut:
fOut.write(f'{username}:{salt}${hash}\n')
def log_new_user_unsalted(username, user_password):
hash = make_unsalted_hash(user_password)
with open(NO_SALT_FILE, 'a') as fOut:
fOut.write(f'{username}:{hash}\n')
def make_unsalted_hash(password):
return hash_algo('sha256', password.encode('utf-8'), bytes(), 100000)
def encrypt_bytes(nons, hash) -> bytes:
fer = Fernet(
base64.urlsafe_b64encode(
hash
)
)
return fer.encrypt(
base64.urlsafe_b64encode(
nons.encode()
)
)
def encrypt_bytes_from(orgBytes, encryption_key):
key = base64.urlsafe_b64encode(encryption_key)
fer = Fernet(key)
return fer.encrypt(orgBytes)
def decrypt_bytes(orgBytes, encryption_key):
fer = Fernet(
base64.urlsafe_b64encode(
encryption_key
)
)
return base64.urlsafe_b64decode(
fer.decrypt(
orgBytes
)
).decode()