Support Forums

Full Version: User login and register script
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
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:
Code:
print 'You have successfully logged in!'
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.
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
Blackhat
Hehe, because you couldnt have just used sqlite3 right? Tongue
Bookmarked so that i can read more tomorrow. Big Grin

Good explanation of it also.
(04-05-2010, 06:19 PM)Fallen Wrote: [ -> ]Hehe, because you couldnt have just used sqlite3 right? Tongue
Exactly, got to make things extra complicated for no reason. It was more of a concept than a useful script.
Nice. I think I'm going to need something similar. But quite different. Basically the same thing, but where the data is saved in a MySQL database c: But it's gonna take ages because this will need to interact with browsers and vice-versa.
Good script, but needs to be organized a bit more.
Thank you so much man !
(12-12-2010, 04:35 PM)Blacklite Wrote: [ -> ]Good script, but needs to be organized a bit more.

Agreed, nice work, could be fixed up. If you need any help just ask us [either on here or when HF is back up]

-Wyatt
Good job man Thnaks
(12-13-2010, 11:00 PM)Bursihido Wrote: [ -> ]Good job man Thnaks

You've got a illuminti signature man. "666". I dont know if u realized that.
Pages: 1 2