02-25-2010, 10:41 PM
I saw one of these over at leetcoders, so I thought I would give it a shot. I figured out a way to keep updating a dictionary via advanced file iteration. All you have to do is put a data.txt file in the same folder as the program and it will work. Features and SHA512 encryption. To implement into your programs (RPG's, etc.) simply call your programs main function after this line:
I havn't made a program like this in a while and it was a lot of fun. Made me more familiar with file iteration, dictionaries, encryption, and indexing, here is the source.
If you have any questions about the program reply to this thread, PM me, or add me on msn.
If you use this program in your projects just give me some credit
Code:
print 'You have successfully logged in!'
Code:
#Published by uber1337, read comments on how to implement
import hashlib, time
#User database, updated on runtime
database = {}
# Returns an encrypted string
def encrypt(string):
crypt = hashlib.sha512()
crypt.update(string)
return crypt.hexdigest()
#First update database with a complex file iteration
def update():
global database
file = open('data.txt')
y = file.readlines()
for x in range(len(y)):
try:
if x == 1:
strip = str(y[0])
strip = strip.replace('\n', '')
strip = strip.split(':')
database[strip[0]]=strip[1]
if y[1]:
strip = str(y[1])
strip = strip.replace('\n', '')
strip = strip.split(':')
database[strip[0]]=strip[1]
else:
pass
else:
strip = str(y[x])
strip = strip.replace('\n', '')
strip = strip.split(':')
database[strip[0]]=strip[1]
except (IndexError):
f = open('data.txt', 'w')
f.write('')
menu()
f.close()
file.close()
#Basic menu
def menu():
print 'Welcome to the Main Page\n'
print '1.) Login\n'
print '2.) Register\n'
choice = int(raw_input('What would you like to do?(1 or 2):'))
if choice == 1:
login()
elif choice == 2:
register()
else:
print 'You have entered an incorrect choice'
time.sleep(1)
menu()
#Login function, add your own function after the "You have successfully logged in" message to implement
def login():
global database
print '\n Hello, welcome to the login page\n'
user = str(raw_input('Please enter your username:\n'))
user = encrypt(user)
if database.has_key(user):
password = str(raw_input('\nHello, please enter your password:' ))
password = encrypt(password)
if database[user] == password:
print 'You have successfully logged in!'
else:
print 'You have entered an incorrect password, choose one of the following'
print '\n 1.) Try again'
print '\n 2.) Return to the main menu'
choice = int(raw_input('What would you like to do?(1 or 2):'))
if choice == 1:
login()
elif choice == 2:
menu()
else:
print 'You have entered an incorrect answer, returning you to the menu...\n'
time.sleep(1)
else:
print 'You have entered a non existant username, choose one of the following:\n'
print '1.) Try again'
print '2.) Register'
print '3.) Return to the main menu'
choice = int(raw_input('What would you like to do?(1, 2 or 3):'))
if choice == 1:
login()
elif choice == 2:
register()
elif choice == 3:
menu()
else:
print 'You have entered an incorrect choice, returning you to the menu...'
time.sleep(1)
menu()
#Register function
def register():
global database
print '\n Welcome to the registeration page!\n'
user = str(raw_input('Please enter a new username:'))
user = encrypt(user)
if database.has_key(user):
print 'User name exists, please enter a new one\n'
register()
elif user == '':
print 'Invalid Username...\n'
register()
else:
password = str(raw_input('Please create a new password for your account:'))
if password == '':
print 'Invalid password'
register()
else:
password = encrypt(password)
database[user] = password
f = open('data.txt', 'a+')
string = user + ':' + password + '\n'
f.write(string)
f.close()
print 'You have successfully created your account!, what would you like to do?'
print '\n 1.) Return to the menu'
print '\n 2.) Login to your account'
choice = int(raw_input('What would you like to do?(1 or 2):'))
if choice == 1:
menu()
elif choice == 2:
login()
else:
print 'You have typed an incorrect answer, returning you to the menu...'
time.sleep(1)
menu()
if __name__ == '__main__':
if open('data.txt').read() == '':
menu()
else:
update()
menu()
If you have any questions about the program reply to this thread, PM me, or add me on msn.
If you use this program in your projects just give me some credit