Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def login():
- import getpass
- import os
- import time
- import platform
- import hashlib
- import json
- from Crypto.Cipher import AES
- from Crypto.Random import get_random_bytes
- import base64
- import json
- if 'Windows' in platform.system():
- os.system('cls')
- else:
- os.system('clear')
- data = {
- }
- secret = get_random_bytes(16)
- cipher = AES.new(secret, AES.MODE_ECB) # secret is a 16 byte key that you create
- def encrypt_json(cipher, json_data):
- json_string = json.dumps(json_data).encode()
- return base64.b64encode(cipher.encrypt(json_string))
- def decrypt_json(cipher, encrypted_data):
- return cipher.decrypt(base64.b64decode(encrypted_data))
- with open(".users.json", 'rb') as file:
- data = file.read()
- data = json.loads(decrypt_json(cipher, data))
- encrypted = encrypt_json(cipher, json_data) # encrypt the data
- decrypted = decrypt_json(cipher, encrpyted)
- with open(".users.json", 'wb+') as file:
- file.write(encrypted)
- # here is the part to choose login or add new user or exit
- choice1 = False
- choice2 = False
- choice3 = False
- choice4 = False
- while not choice1:
- print('Choose an option:')
- print('[1]login')
- print('[2]add user')
- print('[3]exit')
- choice = input('enter the number here: ')
- if choice == '1':
- choice1 = True
- elif choice == '2':
- # adding new user and getting passwords and name (isnt fixed 100%)
- if 'Windows' in platform.system():
- os.system('cls')
- else:
- os.system('clear')
- new_usr = input('enter new username: ')
- new_usr_pass = getpass.getpass('enter new password: ')
- new_usr_pass2 = getpass.getpass('re-enter new password: ')
- while not choice2:
- if len(new_usr_pass) >= 8:
- choice2 = True
- choice3 = True
- else:
- if 'Windows' in platform.system():
- os.system('cls')
- else:
- os.system('clear')
- print('password is to short you need at least 8 digits')
- time.sleep(2.5)
- new_usr_pass = getpass.getpass('enter new password: ')
- new_usr_pass2 = getpass.getpass('re-enter new password: ')
- new_usr_pass_encode = new_usr_pass.encode()
- new_usr_pass2_encode = new_usr_pass2.encode()
- new_usr_pass_hash = hashlib.sha3_512(new_usr_pass_encode).hexdigest()
- new_usr_pass2_hash = hashlib.sha3_512(new_usr_pass2_encode).hexdigest()
- while not choice3:
- if new_usr_pass_hash == new_usr_pass2_hash:
- choice3 = True
- else:
- print('passwords DON´T match')
- time.sleep(1)
- if 'Windows' in platform.system():
- os.system('cls')
- else:
- os.system('clear')
- new_usr_pass = getpass.getpass('enter new password: ')
- new_usr_pass2 = getpass.getpass('re-enter new password: ')
- while not choice4:
- if len(new_usr_pass) >= 7:
- choice4 = True
- else:
- if 'Windows' in platform.system():
- os.system('cls')
- else:
- os.system('clear')
- print('password is to short you need at least 8 digits')
- time.sleep(2.5)
- new_usr_pass = getpass.getpass('enter new password: ')
- new_usr_pass2 = getpass.getpass('re-enter new password: ')
- new_usr_pass_encode = new_usr_pass.encode()
- new_usr_pass2_encode = new_usr_pass2.encode()
- new_usr_pass_hash = hashlib.sha3_512(new_usr_pass_encode).hexdigest()
- new_usr_pass2_hash = hashlib.sha3_512(new_usr_pass2_encode).hexdigest()
- if new_usr not in usname:
- encrypt_json()
- usname[new_usr] = new_usr_pass_hash
- else:
- if 'Windows' in platform.system():
- os.system('cls')
- else:
- os.system('clear')
- print('user is allready existing')
- time.sleep(2)
- if 'Windows' in platform.system():
- os.system('cls')
- else:
- os.system('clear')
- exit()
- if 'Windows' in platform.system():
- os.system('cls')
- else:
- os.system('clear')
- print('succesfully added new user!')
- time.sleep(2)
- if 'Windows' in platform.system():
- os.system('cls')
- else:
- os.system('clear')
- elif choice == '3':
- shutdown()
- # login stuff
- if 'Windows' in platform.system():
- os.system('cls')
- else:
- os.system('clear')
- username = input('enter username: ')
- pword= usname.get(username.encode(), None)
- if not pword:
- print("Incorrect credentials")
- exit()
- if username in usname:
- right_usn = True
- else:
- print('Wrong username!')
- right_usn = False
- time.sleep(1)
- if 'Windows' in platform.system():
- os.system('cls')
- else:
- os.system('clear')
- if right_usn == True:
- pass
- else:
- username = input('re-enter username: ')
- pword= usname.get(username, None)
- if not pword:
- print("Incorrect credentials")
- exit()
- if username in usname:
- pass
- else:
- print('Wrong username again!')
- time.sleep(1)
- if 'Windows' in platform.system():
- os.system('cls')
- else:
- os.system('clear')
- shutdown()
- password = getpass.getpass('enter password: ')
- password_encode = password.encode()
- password_hash = hashlib.sha3_512(password_encode).hexdigest()
- if password_hash == pword:
- pass
- else:
- if 'Windows' in platform.system():
- os.system('cls')
- else:
- os.system('clear')
- print('Wrong password!')
- time.sleep(1)
- shutdown()
- return
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement