Jump to content
Gonzalez

[python] Login System with MD5 Encryption

Recommended Posts

Posted
#!/usr/bin/env python
#Author: s3my0n
#Base Idea: 1R3N1CU5

#Comment: If you feed invalid type of database, the program will break ^^ be nice

import sys
from hashlib import md5

class 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 database

def about():
a = '''
#########################
# #
# Author: s3my0n #
# Idea: 1R3N1CU5 #
# #
# For Intern0t.net !!! #
# #
# Experiment and Learn #
# #
#########################
'''
return a

def rules():
r = '''
1: Register new user
2: Login with existing user
3: Exit the program
'''
return r

def 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 database

def 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

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



×
×
  • Create New...