Advertisement
fabilus11

login

Jan 7th, 2019
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 7.48 KB | None | 0 0
  1. def login():
  2.     import getpass
  3.     import os
  4.     import time
  5.     import platform
  6.     import hashlib
  7.     import json
  8.     from Crypto.Cipher import AES
  9.     from Crypto.Random import get_random_bytes
  10.     import base64
  11.     import json
  12.    
  13.     if 'Windows' in platform.system():
  14.         os.system('cls')
  15.     else:
  16.         os.system('clear')
  17.    
  18.     data = {
  19.  
  20. }
  21.  
  22.     secret = get_random_bytes(16)
  23.     cipher = AES.new(secret, AES.MODE_ECB)  # secret is a 16 byte key that you create
  24.  
  25.  
  26.  
  27.     def encrypt_json(cipher, json_data):
  28.         json_string = json.dumps(json_data).encode()
  29.         return base64.b64encode(cipher.encrypt(json_string))
  30.  
  31.  
  32.     def decrypt_json(cipher, encrypted_data):
  33.         return cipher.decrypt(base64.b64decode(encrypted_data))
  34.    
  35.     with open(".users.json", 'rb') as file:
  36.         data = file.read()
  37.         data = json.loads(decrypt_json(cipher, data))
  38.    
  39.     encrypted = encrypt_json(cipher, json_data)  # encrypt the data
  40.     decrypted = decrypt_json(cipher, encrpyted)
  41.    
  42.     with open(".users.json", 'wb+') as file:
  43.         file.write(encrypted)
  44.  
  45. # here is the part to choose login or add new user or exit
  46.  
  47.     choice1 = False
  48.     choice2 = False
  49.     choice3 = False
  50.     choice4 = False
  51.  
  52.     while not choice1:
  53.         print('Choose an option:')
  54.         print('[1]login')
  55.         print('[2]add user')
  56.         print('[3]exit')
  57.         choice = input('enter the number here: ')
  58.    
  59.         if choice == '1':
  60.             choice1 = True
  61.         elif choice == '2':
  62. # adding new user and getting passwords and name (isnt fixed 100%)
  63.                 if 'Windows' in platform.system():
  64.                     os.system('cls')
  65.                 else:
  66.                     os.system('clear')
  67.                 new_usr = input('enter new username: ')
  68.                 new_usr_pass = getpass.getpass('enter new password: ')
  69.                 new_usr_pass2 = getpass.getpass('re-enter new password: ')
  70.            
  71.                 while not choice2:
  72.  
  73.                     if len(new_usr_pass) >= 8:
  74.                         choice2 = True
  75.                         choice3 = True
  76.                     else:
  77.                         if 'Windows' in platform.system():
  78.                             os.system('cls')
  79.                         else:
  80.                             os.system('clear')
  81.                     print('password is to short you need at least 8 digits')
  82.                     time.sleep(2.5)
  83.                     new_usr_pass = getpass.getpass('enter new password: ')
  84.                     new_usr_pass2 = getpass.getpass('re-enter new password: ')
  85.                    
  86.                     new_usr_pass_encode = new_usr_pass.encode()
  87.                     new_usr_pass2_encode = new_usr_pass2.encode()
  88.                     new_usr_pass_hash = hashlib.sha3_512(new_usr_pass_encode).hexdigest()
  89.                     new_usr_pass2_hash = hashlib.sha3_512(new_usr_pass2_encode).hexdigest()
  90.  
  91.                     while not choice3:
  92.                         if new_usr_pass_hash == new_usr_pass2_hash:
  93.                             choice3 = True
  94.                         else:
  95.                             print('passwords DON´T match')
  96.                             time.sleep(1)
  97.                             if 'Windows' in platform.system():
  98.                                 os.system('cls')
  99.                             else:
  100.                                 os.system('clear')
  101.                             new_usr_pass = getpass.getpass('enter new password: ')
  102.                             new_usr_pass2 = getpass.getpass('re-enter new password: ')
  103.  
  104.                         while not choice4:
  105.  
  106.                             if len(new_usr_pass) >= 7:
  107.                                 choice4 = True
  108.                             else:
  109.                                 if 'Windows' in platform.system():
  110.                                     os.system('cls')
  111.                                 else:
  112.                                     os.system('clear')
  113.                                 print('password is to short you need at least 8 digits')
  114.                                 time.sleep(2.5)
  115.                                 new_usr_pass = getpass.getpass('enter new password: ')
  116.                                 new_usr_pass2 = getpass.getpass('re-enter new password: ')
  117.                    
  118.                         new_usr_pass_encode = new_usr_pass.encode()
  119.                         new_usr_pass2_encode = new_usr_pass2.encode()
  120.                         new_usr_pass_hash = hashlib.sha3_512(new_usr_pass_encode).hexdigest()
  121.                         new_usr_pass2_hash = hashlib.sha3_512(new_usr_pass2_encode).hexdigest()
  122.                    
  123.                     if new_usr not in usname:
  124.                         encrypt_json()
  125.                         usname[new_usr] = new_usr_pass_hash
  126.                     else:
  127.                         if 'Windows' in platform.system():
  128.                             os.system('cls')
  129.                         else:
  130.                             os.system('clear')
  131.                        
  132.                         print('user is allready existing')
  133.                         time.sleep(2)
  134.                        
  135.                         if 'Windows' in platform.system():
  136.                             os.system('cls')
  137.                         else:
  138.                             os.system('clear')
  139.                        
  140.                         exit()
  141.                    
  142.                    
  143.                     if 'Windows' in platform.system():
  144.                         os.system('cls')
  145.                     else:
  146.                         os.system('clear')
  147.                            
  148.                     print('succesfully added new user!')
  149.                     time.sleep(2)
  150.                    
  151.                     if 'Windows' in platform.system():
  152.                         os.system('cls')
  153.                     else:
  154.                         os.system('clear')
  155.         elif choice == '3':
  156.             shutdown()
  157.    
  158.     # login stuff
  159.    
  160.     if 'Windows' in platform.system():
  161.         os.system('cls')
  162.     else:
  163.         os.system('clear')
  164.    
  165.     username = input('enter username: ')
  166.     pword= usname.get(username.encode(), None)
  167.  
  168.     if not pword:
  169.         print("Incorrect credentials")
  170.         exit()
  171.  
  172.     if username in usname:
  173.         right_usn = True
  174.     else:
  175.         print('Wrong username!')
  176.         right_usn = False
  177.         time.sleep(1)
  178.         if 'Windows' in platform.system():
  179.             os.system('cls')
  180.         else:
  181.             os.system('clear')
  182.  
  183.     if right_usn == True:
  184.         pass
  185.     else:
  186.         username = input('re-enter username: ')  
  187.         pword= usname.get(username, None)
  188.  
  189.         if not pword:
  190.             print("Incorrect credentials")
  191.             exit()
  192.            
  193.         if username in usname:
  194.             pass
  195.         else:  
  196.             print('Wrong username again!')
  197.             time.sleep(1)
  198.             if 'Windows' in platform.system():
  199.                 os.system('cls')
  200.             else:
  201.                 os.system('clear')
  202.             shutdown()
  203.  
  204.     password = getpass.getpass('enter password: ')
  205.     password_encode = password.encode()
  206.     password_hash = hashlib.sha3_512(password_encode).hexdigest()
  207.    
  208.     if password_hash == pword:
  209.         pass
  210.     else:
  211.         if 'Windows' in platform.system():
  212.             os.system('cls')
  213.         else:
  214.             os.system('clear')
  215.         print('Wrong password!')
  216.         time.sleep(1)
  217.         shutdown()
  218.     return
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement