import hashlib import json import os import time import secrets import string def load_db(db_path): # If the file provided does not exist, return and empty dict (a blank database) if not os.path.isfile(db_path): return {} with open(db_path, 'r') as reader: db = reader.read() return json.loads(db) def pwd(db_pwd_path): if not os.path.isfile(db_pwd_path): return {} with open(db_pwd_path, 'r') as reader: db = reader.read() return json.loads(db) def generator(db_pwd_path): time.sleep(1) print("Alright, let's begin") db = pwd(db_pwd_path) complex_level = input("How hard do you want your password to be? 'easy' 'medium' 'hard': ") complex_level = complex_level.lower() alphabet = string.ascii_letters + string.digits + string.punctuation pass_word = secrets.choice(string.ascii_lowercase) pass_word += secrets.choice(string.ascii_uppercase) pass_word += secrets.choice(string.digits) pass_word += secrets.choice(string.punctuation) if complex_level == 'easy': for i in range(2): pass_word += secrets.choice(alphabet) char_list = list(pass_word) secrets.SystemRandom().shuffle(char_list) pass_word = ''.join(char_list) print("Secure Password is: ", pass_word) print("nice") elif complex_level == 'medium': for i in range(6): pass_word += secrets.choice(alphabet) char_list = list(pass_word) secrets.SystemRandom().shuffle(char_list) pass_word = ''.join(char_list) print("Secure Password is: ", pass_word) print("alright") elif complex_level == 'hard': for i in range(2): pass_word += secrets.choice(alphabet) char_list = list(pass_word) secrets.SystemRandom().shuffle(char_list) pass_word = ''.join(char_list) print("Secure Password is: ", pass_word) print("wow, calm down") else: print("Pick 'easy' or 'medium' or 'hard' next time") time.sleep(1) breakpoint() website_info = {'website': input("Enter website/program for the password we just generated: "), 'password': pass_word} db[website_info['website']] = website_info # db[website_info['password']] = pass_word # Update the database file with open(db_pwd_path, 'w') as writer: print('Updating the contents of the database') print(db) writer.write(json.dumps(db)) def login(db_path): print("Password Generator v2\n Sign in please:") username = input("Enter username: ") password = input("Enter password: ") db = load_db(db_path) valid = True # 1. Does the provided username exist? if db.get(username): user_pass = db.get(username)['secret'] # 2. Ok, we know the user's hashed password. Now let's hash the input and compare: if hashlib.sha224(password.encode('utf-8')).hexdigest() != user_pass: valid = False else: valid = False if valid: generator('pwd.json') else: print("Invalid Password! or Username!") def register(db_path): selection = input("Please choose: 'register' or 'login' ") selection = selection.lower() # makes whatever input the user puts lowercase if selection == 'register': user_info = {'name': input("Enter Username? "), 'secret': input("Enter Password? "), 'email': input("Enter Email? ")} db = load_db(db_path) # check to see if name is already taken if db.get(user_info['name']): raise Exception('Username already taken ({})'.format(user_info['name'])) # if it's not taken, create a slot for it in the DataBase/db db[user_info['name']] = user_info # don't forget to hash the password user_info['secret'] = hashlib.sha224(user_info['secret'].encode('utf-8')).hexdigest() # Update the database file with open(db_path, 'w') as writer: print('Updating the contents of the database') print(db) writer.write(json.dumps(db)) elif selection == 'login': login(db_path) else: print("Invalid option. Please enter 'register' or 'login'") register('user_db.json')
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question