The getpass
module is written in Python. You could easily modify it to do this. In fact, here is a modified version of getpass.win_getpass()
that you could just paste into your code:
import sys
def win_getpass(prompt='Password: ', stream=None):
"""Prompt for password with echo off, using Windows getch()."""
import msvcrt
for c in prompt:
msvcrt.putch(c)
pw = ""
while 1:
c = msvcrt.getch()
if c == '
' or c == '
':
break
if c == '03':
raise KeyboardInterrupt
if c == '':
pw = pw[:-1]
msvcrt.putch('')
else:
pw = pw + c
msvcrt.putch("*")
msvcrt.putch('
')
msvcrt.putch('
')
return pw
You might want to reconsider this, however. The Linux way is better; even just knowing the number of characters in a password is a significant hint to someone who wants to crack it.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…