silent raw_input for passwords

Dominique Orban do at linux.darktower.net
Sun May 2 10:03:48 EDT 2004


On 2004-05-01, Dan Bishop <danb_83 at yahoo.com> wrote:
> Stephen Boulet <stephen.no at spam.theboulets.net.please> wrote in message news:<B36dncCgsNfVkQ7d3cwC-g at speakeasy.net>...
>> I need a password for a script and I would like to not have it stored in a
>> file or shown in a terminal.
>> 
>> "passphrase = raw_input()" still lets you see the input on the screen. Is
>> there a way to have it be hidden? It's my gpg passphrase, so I don't want
>> it anywhere except in my head.
>
> There's probably an easier way, but you can use:
>
> def input_password(echo=True):
>    chars = []
>    while True:
>       newChar = getch()
>       if newChar in '\r\n':
>          break
>       elif newChar in '\b\x7F':
>          if chars:
>             del chars[-1]
>          sys.stdout.write('\b')
>       else:
>          chars.append(newChar)
>          if echo:
>             sys.stdout.write('*')
>    return ''.join(chars)

How is this different from getpass.getpass() ?




More information about the Python-list mailing list