salting_fix #16
1 changed files with 36 additions and 2 deletions
|
@ -1,17 +1,51 @@
|
|||
import getpass # hides types characters, very useful
|
||||
import json
|
||||
import sys
|
||||
import hashlib
|
||||
import random
|
||||
import string
|
||||
|
||||
|
||||
def get_hash(password,salt):
|
||||
|
||||
hash_ = str(hashlib.sha256((password+salt).encode('utf-8')).hexdigest())
|
||||
return hash_
|
||||
|
||||
def get_salt(char_num=10):
|
||||
"""Create random string of characters
|
||||
|
||||
Parameters
|
||||
----------
|
||||
char_num: int.
|
||||
Number of random characters to be created.
|
||||
"""
|
||||
|
||||
salt = ''.join(random.choices(string.ascii_uppercase + string.digits, k=char_num))
|
||||
|
||||
return salt
|
||||
|
||||
def create_hash(password):
|
||||
|
||||
salt = get_salt(10)
|
||||
|
||||
hash_ = str(hashlib.sha256((password+salt).encode('utf-8')).hexdigest())
|
||||
return hash_, salt
|
||||
|
||||
def get_credentials():
|
||||
username = input('Enter your username: ')
|
||||
password = getpass.getpass('Enter your password: ')
|
||||
|
||||
return (username, password)
|
||||
|
||||
def authenticate(username, password, pwdb):
|
||||
return password == pwdb[username]
|
||||
salt = pwdb[username][1]
|
||||
given_hash = get_hash(password, salt)
|
||||
return given_hash == pwdb[username][0]
|
||||
|
||||
def add_user(username, pwdb):
|
||||
pwdb[username] = getpass.getpass(f'Enter password for {username}: ')
|
||||
password = getpass.getpass(f'Enter password for {username}: ')
|
||||
hash_, salt = create_hash(password)
|
||||
pwdb[username] = (hash_, salt)
|
||||
return pwdb
|
||||
|
||||
def read_pwdb(pwdb_path):
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue