Last active
December 25, 2015 13:39
-
-
Save TylerLeite/6985160 to your computer and use it in GitHub Desktop.
Simple login / registration script.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from datetime import datetime | |
from hashlib import * | |
from bcrypt import hashpw, gensalt | |
def super_hash_bros(string, salt): | |
if salt is None: | |
salt = gensalt() | |
return (hashpw(string, salt), salt) # not technically how this is used, | |
# salt already returned by hashpw | |
def make_entry(filename, eid, username, password, email, name): | |
date = datetime.now() | |
password, salt = super_hash_bros(password, None) | |
entry = "%i~%i~%s~%s~%s~%s~%s~%s~\n" %\ | |
(eid, 0, date, username, password, email, name, salt) | |
with open(filename, "a") as database: | |
database.writelines(entry) | |
def get_db(filename): | |
usernames, passwords = list(), list() | |
with open(filename, "r") as database: | |
for next_id, line in enumerate(database): | |
arr = line.split("~") | |
usernames.append(arr[3]) | |
passwords.append(arr[4] + "~" + arr[7]) | |
database = dict(zip(usernames, passwords)) | |
return database | |
def register(filename, next_id): | |
database = get_db(filename) | |
def get_username(): | |
errors = str() | |
username = raw_input("Enter your desired username: ").lower() | |
if not username.isalnum(): | |
errors += "\tUsername may only contain alphanumeric characters.\n" | |
if username in database.keys(): | |
errors += "\tUsername is taken.\n" | |
if len(username) > 16: | |
errors += "\tUsername may be no longer than 16 characters.\n" | |
if errors: | |
print "The following errors occurred:\n" + errors | |
get_username() | |
else: | |
return username | |
def get_password(): | |
errors = str() | |
password = raw_input("Enter your desired password: ").lower() | |
password_confirm = raw_input("Confirm your password: ").lower() | |
if not password.isalnum(): | |
errors += "\tPassword may only contain alphanumeric characters.\n" | |
if len(password) > 32: | |
errors += "\tPassword may be no longer than 32 characters.\n" | |
if password != password_confirm: | |
errors += "\tPasswords did not match.\n" | |
if errors: | |
print "The following errors occurred:\n" + errors | |
get_password() | |
else: | |
return password | |
def get_email(): | |
errors = str() | |
email = raw_input("Enter your email: ").lower() | |
email_confirm = raw_input("Confirm your email: ").lower() | |
if not email: | |
return str() | |
if email.count("@") != 1 or email[-1] == ".": | |
errors += "\tInvalid email.\n" | |
if email != email_confirm: | |
errors += "\tPasswords did not match.\n" | |
if errors: | |
print "The following errors occurred:\n" + errors | |
get_email() | |
else: | |
return email | |
def get_name(): | |
errors = str() | |
name = raw_input("Enter your full name: ").title() | |
if not name.replace(" ", "").isalnum(): | |
errors += "\tName is not plausible.\n" | |
if errors: | |
print "The following errors occurred:\n" + errors | |
get_name() | |
else: | |
return email | |
username = get_username() | |
password = get_password() | |
email = get_email() | |
name = get_name() | |
make_entry(filename, next_id, username, password, email, name) | |
return next_id + 1 | |
def login(): | |
database = get_db("people.db") | |
username = raw_input("Username: ") | |
password = raw_input("Password: ") | |
password_parts = database[username].split("~") | |
salt = password_parts[1] | |
password_in_db = password_parts[0] | |
if username in database.keys() and super_hash_bros(password, salt) == password_in_db: | |
print "You got in!" | |
else: | |
print "Incorrect username / password combination." | |
def main(): | |
next_id = 0 | |
while True: | |
cmd = raw_input("Do you want to login or register? ").lower() | |
if cmd in "stop exit quit".split(): | |
break | |
elif cmd == "register": | |
next_id = register("people.db", next_id) | |
elif cmd == "login": | |
login() | |
else: | |
print "Unknown op: %s" % cmd | |
print "Goodbye!" | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment