Data entry without echoing on screen

Asked

Viewed 337 times

4

entrada = input("digite a senha")

If I use the function input, what the user keystrokes will be echoed on the screen. How to do so so that nothing is shown on the screen?

2 answers

5


With the method input() I don’t think so. But you can do it like 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 verification 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 safer 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')
  • Thanks, @Miguel. And if I want to not have the password in the code but rather a hash or something like that to do if, there’s a simple way to do?

  • Of course, give me a sec that I already put an example @Paulsigonoso

  • Warning (from warnings module): File "/usr/lib/python3.4/getpass.py", line 63 passwd = fallback_getpass(prompt, stream) Getpasswarning: Can not control echo on the terminal. Warning: Password input may be echoed.

  • Here the password was echoed! I used IDLE

  • thanks. Sorry for the amount of questions!

  • It doesn’t matter @Paulsigonoso

  • Edited @Paulsigonoso , note that the hash will never be like this in your code, read the comment ahead

  • Strange that keeps echoing on the screen: Warning (from warnings module): File "/usr/lib/python3.4/getpass.py", line 63 passwd = fallback_getpass(prompt, stream) Getpasswarning: Can’t control echo on the terminal. Warning: Password input may be echoed.

  • @Paulsigonoso, strange... Here it works, both in py2 and py3, only the last example is that it only works in python3.x

  • I’m using IDLE: Python 3.4.3 (default, Oct 14 2015, 20:28:29) [GCC 4.8.4] on linux

  • Ha, try running in the terminal: like this, python3 caminho/para/este/script.py @Paulsigonoso

  • Warning (from warnings module): File "/usr/lib/python3.4/getpass.py", line 63 passwd = fallback_getpass(prompt, stream) Getpasswarning: Can not control echo on the terminal. Warning: Password input may be echoed. enter password 123

  • This probably depends on terminal support.

  • 'Cause maybe this is it @Pabloalmeida, I just tested on the terminal

Show 9 more comments

2

A very simple way to do this is to put to record in os.devnull, so it will be possible to 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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.