Data entry without echoing on the screen

4
entrada = input("digite a senha")

If I use the input function, what the user types will be echoed on the screen. How to make sure nothing is shown on the screen?

    
asked by anonymous 10.08.2016 / 17:36

2 answers

5

With the input() method, I do not think so. But you can do this:

import getpass

p = getpass.getpass(prompt='digite a senha\n')
if p == '12345':
    print('YO Paul')
else:
    print('BRHHH')
print('O seu input foi:', p) # p = seu input

Simulating the check of a hash:

import hashlib, getpass

key_string = b"12345"
hashed = hashlib.md5(key_string).hexdigest() # a hash vai estar numa base de dados/ficheiro num projeto real

p = getpass.getpass(prompt='digite a senha\n')
if hashlib.md5(p.encode('utf-8')).hexdigest() == hashed:
    print('YO Paul')
else:
    print('BRHHH')

A significantly more secure way (python 3.x) :

import hashlib, binascii, getpass

password = "12345"
# hashlib.pbkdf2_hmac('metodo de hash', 'password', 'salt', 'custo/aumento de entropia')
pw = hashlib.pbkdf2_hmac('sha256', password.encode('utf-8'), b'salt' , 100000)
hashed_pw = binascii.hexlify(pw)

p = getpass.getpass(prompt='digite a senha\n')
input_hashed_pw = hashlib.pbkdf2_hmac('sha256', p.encode('utf-8'), b'salt', 100000)
if binascii.hexlify(input_hashed_pw) == hashed_pw:
    print('YO Paul')
else:
    print('BRHHH')
    
10.08.2016 / 17:40
2

A very simple way to do this is to write to os.devnull , so you can use input :

import sys, os

#Salva o output
output_salvo = sys.stdout

# Define o ouput pra gravar no devnull    
f = open(os.devnull, 'w')
sys.stdout = f

entrada = input("digite a senha")

# Restaura o output
sys.stdout = output_salvo
    
10.08.2016 / 19:33