implement hashing method using hashlib #16
12
auth.py
12
auth.py
|
@ -1,19 +1,27 @@
|
||||||
import json
|
import json
|
||||||
import sys
|
import sys
|
||||||
from getpass import getpass
|
from getpass import getpass
|
||||||
|
import hashlib
|
||||||
|
|
||||||
PWDB_PATH = 'pwdb.json'
|
PWDB_PATH = 'pwdb.json'
|
||||||
|
|
||||||
|
def hashing(password):
|
||||||
|
pwd_string = bytes(str(password), 'utf-8')
|
||||||
|
hash_pwd = hashlib.sha256(pwd_string).hexdigest()
|
||||||
|
return hash_pwd
|
||||||
|
|
||||||
def get_credentials():
|
def get_credentials():
|
||||||
username = input('Enter your username: ')
|
username = input('Enter your username: ')
|
||||||
password = getpass('Enter your password: ')
|
password = getpass('Enter your password: ')
|
||||||
return (username, password)
|
hash_pwd = hashing(password)
|
||||||
|
return (username, hash_pwd)
|
||||||
|
|
||||||
def authenticate(username, password, pwdb):
|
def authenticate(username, password, pwdb):
|
||||||
return password == pwdb[username]
|
return password == pwdb[username]
|
||||||
|
|
||||||
def add_user(username, pwdb):
|
def add_user(username, pwdb):
|
||||||
pwdb[username] = input(f'Enter password for {username}: ')
|
password = input(f'Enter password for {username}: ')
|
||||||
|
pwdb[username] = hashing(password)
|
||||||
return pwdb
|
return pwdb
|
||||||
|
|
||||||
def read_pwdb(PWDB_PATH):
|
def read_pwdb(PWDB_PATH):
|
||||||
|
|
Loading…
Reference in a new issue