hash the pass #13
					 1 changed files with 12 additions and 4 deletions
				
			
		
							
								
								
									
										16
									
								
								auth.py
									
										
									
									
									
								
							
							
						
						
									
										16
									
								
								auth.py
									
										
									
									
									
								
							| 
						 | 
				
			
			@ -1,6 +1,7 @@
 | 
			
		|||
import json
 | 
			
		||||
import sys
 | 
			
		||||
from getpass import getpass
 | 
			
		||||
import hashlib
 | 
			
		||||
 | 
			
		||||
PWDB_PATH = 'pwdb.json'
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -10,10 +11,10 @@ def get_credentials():
 | 
			
		|||
    return (username, password)
 | 
			
		||||
 | 
			
		||||
def authenticate(username, password, pwdb):
 | 
			
		||||
    return password == pwdb[username]
 | 
			
		||||
    return pwhash(password) == pwdb[username]
 | 
			
		||||
 | 
			
		||||
def add_user(username, pwdb):
 | 
			
		||||
    pwdb[username] = input(f'Enter password for {username}: ')
 | 
			
		||||
    pwdb[username] = pwhash(input(f'Enter password for {username}: '))
 | 
			
		||||
    return pwdb
 | 
			
		||||
 | 
			
		||||
def read_pwdb(PWDB_PATH):
 | 
			
		||||
| 
						 | 
				
			
			@ -24,6 +25,13 @@ def read_pwdb(PWDB_PATH):
 | 
			
		|||
        pwdb = {}
 | 
			
		||||
    return pwdb
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def pwhash(password):
 | 
			
		||||
    hashed_pass = hashlib.sha256(password.encode('utf-8')).hexdigest()
 | 
			
		||||
    return hashed_pass
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
| 
					
	
 | 
			||||
 | 
			
		||||
def write_pwdb(pwdb, PWDB_PATH):
 | 
			
		||||
    pwdb_file = open(PWDB_PATH, 'wt')
 | 
			
		||||
    json.dump(pwdb, pwdb_file)
 | 
			
		||||
| 
						 | 
				
			
			@ -39,10 +47,10 @@ if __name__ == "__main__":
 | 
			
		|||
    else:
 | 
			
		||||
        username, password = get_credentials()
 | 
			
		||||
        if username not in pwdb:
 | 
			
		||||
            print('Wrong username!')
 | 
			
		||||
            print('Wrong username or password!')
 | 
			
		||||
        else:
 | 
			
		||||
            if authenticate(username, password, pwdb):
 | 
			
		||||
                print('Successfully authenticated!')
 | 
			
		||||
            else:
 | 
			
		||||
                print('Wrong password!')
 | 
			
		||||
                print('Wrong username or password!')
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	
what is this?