seg/trabalho2/gerador.py

74 lines
2.2 KiB
Python

from hashlib import sha256
import localtoken
import os
import time
def init_setup():
with open('gerador.dat', 'a', newline='') as setup:
user = input('Digite o usuário: ')
local_password = sha256(input('Digite a senha local: ').encode('utf-8')).hexdigest()
seed_password = sha256(input('Digite a senha semente: ').encode('utf-8')).hexdigest()
salt = sha256(input('Digite o salt: ').encode('utf-8')).hexdigest()
salted_password = localtoken.get_salted_password(seed_password, salt)
line = f'{user},{local_password},{salted_password}\n'
setup.write(line)
print(f'Usuário [{user}] registrado com sucesso!')
def generate_token():
user = input('Digite o usuário: ')
local_password = sha256(input('Digite a senha local: ').encode('utf-8')).hexdigest()
salted_password = ''
with open('gerador.dat', 'r', newline='') as setup:
for line in setup:
line = line.replace('\n', '')
if line.split(',')[0] == user and line.split(',')[1] == local_password:
salted_password = line.split(',')[2]
break
else:
print('Usuário ou senha incorretos!')
return
loop_token(user, salted_password)
def loop_token(user, salted_password):
while True:
print(f'Gerando token para [{user}]...')
tokens = [localtoken.generate_token(salted_password)]
for i in range(4):
tokens.append(localtoken.generate_token(tokens[-1]))
for token in tokens:
print(token[:8])
time.sleep(60 - time.time() % 60 + 1)
os.system('cls' if os.name == 'nt' else 'clear')
def main():
while True:
print('Selecione uma opção:')
print('1 - Registrar usuário')
print('2 - Gerar token')
print('0 - Sair')
option = input('Opção: ')
if option == '1':
init_setup()
elif option == '2':
generate_token()
elif option == '0':
exit()
def exit():
print('Saindo...')
quit()
if __name__ == '__main__':
if not os.path.exists('gerador.dat'):
open('gerador.dat', 'w').close()
main()