Вот довольно полное переписывание:
import hashlib
import getpass
class Auth(object):
class UserNameTakenError(Exception):
pass
def __init__(self, userDict):
self._users = {name:self.hash(pwd) for name,pwd in userDict.iteritems()}
def hash(self, pw):
return hashlib.sha256(pw+"&@#salt)(string)846!^").digest()
def validate(self, name, pwd):
return (name in self._users) and (self._users[name]==self.hash(pwd))
def create(self, name, pwd):
if name in self._users:
raise Auth.UserNameTakenError()
else:
self._users[name] = self.hash(pwd)
def delete(self, name, pwd):
if self.validate(name, pwd):
del self._users[name]
def getYes(msg):
return raw_input(msg).lower() in ('y','yes')
class App(object):
def __init__(self):
self.auth = Auth({'Robrajow':'password', 'Hacker':'hack', 'Noob':'lololol1'})
self.actions = {
'login': self.doLogin,
'l': self.doLogin,
'register': self.doRegister,
'r': self.doRegister,
'delete': self.doDelete,
'd': self.doDelete,
'exit': self.doExit,
'e': self.doExit,
'x': self.doExit,
'q': self.doExit
}
def welcome(self):
return raw_input("\nWelcome to Telemology! You can Login, Register, Delete an account, or Exit.\nWhat would you like to do? ")
def doLogin(self):
name = raw_input("What is your username? ")
pwd = getpass.getpass("What is your password? ")
if self.auth.validate(name, pwd):
print "Welcome to Telemology, {0}".format(name)
else:
print "The username or password you entered is incorrect. Please try again or register."
return False
def doRegister(self):
name = raw_input("What is your new username? ")
pwd = getpass.getpass("What is your new password? ")
try:
self.auth.create(name, pwd)
print "{0}, welcome to Telemology!".format(name)
except Auth.UserNameTakenError:
print "That account name is already taken. Please try again."
return False
def doDelete(self):
name = raw_input("What is your username? ")
pwd = getpass.getpass("What is your password? ")
if self.auth.validate(name, pwd):
if getYes("Do you really want to delete your account? It will never return! "):
self.auth.delete(name, pwd)
print "Goodbye forever, {0}".format(name)
else:
print "I hoped so."
else:
print "The username or password you entered is incorrect."
return False
def doExit(self):
print "Goodbye!"
return True
def run(self):
while True:
act = self.welcome().lower()
if act in self.actions:
if self.actions[act]():
break
else:
print "What? You can only input login, delete, register, or exit."
def main():
myapp = App()
myapp.run()
if __name__=="__main__":
main()