Skip to content

Commit 2d95809

Browse files
Add password hashing and verification functions using bcrypt
* Add `generate_password` function to hash passwords * Add `verify_password` function to check hashed passwords * Add `generate_random_number` function using secrets module * Add `generate_random_string` function using secrets module
1 parent 6d7d343 commit 2d95809

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

utils.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import bcrypt
2+
import secrets
3+
4+
def generate_password(password):
5+
return bcrypt.hashpw(password.encode('utf-8'), bcrypt.gensalt()).decode('utf-8')
6+
7+
def verify_password(password, hashed_password):
8+
return bcrypt.checkpw(password.encode('utf-8'), hashed_password.encode('utf-8'))
9+
10+
def generate_random_number(length):
11+
return secrets.randbelow(10**length)
12+
13+
def generate_random_string(length):
14+
return secrets.token_urlsafe(length)

0 commit comments

Comments
 (0)