Gonzalez Posted March 7, 2010 Report Posted March 7, 2010 #!/usr/bin/env python#Author: s3my0n#Base Idea: 1R3N1CU5#Comment: If you feed invalid type of database, the program will break ^^ be niceimport sysfrom hashlib import md5class UserManagement(object): def __init__(self, pathtodb): self.pathtodb = pathtodb def readDatabase(self): try: self.fopen = open(self.pathtodb, 'r+') #tryes to open path except(IOError): print '\n [-] Could not open %s to read/write' % (self.pathtodb) sys.exit() pares = [i.strip() for i in self.fopen.readlines()] #stripping '\n' newline db = {} for i in pares: pare = i.split(':') #this returns [user, pass] out of 'user:pass' db[pare[0]] = pare[1] self.database = db def encryptPassword(self, text): #My nifty md5 hash maker m = md5() m.update(text) en_text = m.hexdigest() return en_text def userLogin(self, user, password): if self.database.has_key(user): if self.database[user] == self.encryptPassword(password): #encrypting password to md5 hash print '\n [+] Access Granted!' #and comparing it to the database's hash else: print '\n [-] Invalid password for %s' % (user) else: print '\n [-] Invalid username' def userRegister(self, user, password): if self.database.has_key(user): print '\n [-] User already exists' return towrite = '%s:%s\n' % (user, self.encryptPassword(password)) self.fopen.write(towrite) #writing 'user:pass' to databasedef about(): a = ''' ######################### # # # Author: s3my0n # # Idea: 1R3N1CU5 # # # # For Intern0t.net !!! # # # # Experiment and Learn # # # ######################### ''' return adef rules(): r = ''' 1: Register new user 2: Login with existing user 3: Exit the program ''' return rdef register(): try: u = raw_input('\n Enter new username: ') p = raw_input('\n Enter new password: ') except (KeyboardInterrupt, IOError): print '\Going back to main menu' return mng.userRegister(u, p) #adding new user mng.readDatabase() #updates databasedef login(): try: u = raw_input('\n Username: ') p = raw_input('\n Password: ') except (KeyboardInterrupt, IOError): print '\n [*] Going back to main menu' return mng.userLogin(u, p)def main(): print rules() while True: try: command = raw_input('\nWhat would you like to do?: ').strip() except (IOError, KeyboardInterrupt): print '\n [*] See ya later aligator' sys.exit() if command in '123': if command == '1': register() if command == '2': login() if command == '3': print '\n [*] BYe..' sys.exit() else: print '\n [-] Invalid command'if __name__=='__main__': while True: try: path = raw_input('\nPlease specify path to user database: ') except (KeyboardInterrupt, IOError): print '\n [*] Aborted' sys.exit() break global mng #this is so all functions can access UserManagement class mng = UserManagement(path) #on this assignment __init__ gets path as pathtodb mng.readDatabase() #reads database print about() main() #starting main function Quote