Advertisement
Guest User

Untitled

a guest
Jan 27th, 2017
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.49 KB | None | 0 0
  1. import hashlib
  2. import time
  3. #cPickle is faster then pickle but not available in all python releases
  4. #thats why i used a try/accept there
  5. try: import cPickle as cp
  6.  
  7.  
  8. #load the database if it exist, if not it create one
  9. try:
  10. f =(r"C:\Users\Owner\Desktop\python\database.data")
  11. data = cp.load(f)
  12. except IOError:
  13. data = {}
  14.  
  15. #A simple function made to make data dumping easy
  16. def easyDump(data_):
  17. f = file(r"C:\Users\Owner\Desktop\python\database.data", "w")
  18. cp.dump(data_, f)
  19. f.close()
  20.  
  21. #Get's the date (We'll use this as the custom salt)
  22. def getData():
  23. return str(time.strftime("%d-%m-%Y"))
  24.  
  25. #A function which accepts two parameters, password and date.
  26. #The date is the custom salt. It returns the sha512 hash excetpyion
  27. def salt(password, date):
  28. salted = hasglib.sha512(password + str(data)).hexdigest()
  29. retun str(salted)
  30.  
  31.  
  32.  
  33.  
  34. menu = """"
  35. 1.Login
  36. 2.Register
  37. 3.Exit
  38. """
  39.  
  40. while True:
  41. print menu
  42. choice = int(raw_input("Your choice please: "))
  43. if choice ==1:
  44. username = raw_input("Enter your username please: ")
  45. password = raw.input("Enter your authentication code please: ")
  46. #if the username is found on the database
  47. if data.has_key(username):
  48. #date is equal to our secured stored data
  49. date = date[username][1]
  50. #check of the given password + date is equal to what is stored on the database
  51. #password
  52. if salt(password, date) == date[username][0]:
  53. print"Welcome %s!" % username
  54. else:
  55. print "Incorrect password"
  56. else:
  57. print "user %s not found, please register!" % username
  58. elif choice == 2:
  59. username = raw_input("Please enter yout username: !")
  60. password = raw_input("Please enter your password: !")
  61. #if username exists in the system already then the name is taken
  62. if data.has_key(username):
  63. print "user %s already registered, please put in another % username
  64. else:
  65. #in order words data = {username: hash, date}
  66. data[username] = [salt(password, getData()), get Data()]
  67. easyDump(data)
  68. print "user %s successfully registereed!" %username
  69. elif choice == 3:
  70. print "goodbye!"
  71. break
  72. else:
  73. print "invaid input or commands"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement