Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Password masking

by arkamedis21 (Acolyte)
on May 02, 2002 at 17:24 UTC ( [id://163606]=perlquestion: print w/replies, xml ) Need Help??

arkamedis21 has asked for the wisdom of the Perl Monks concerning the following question:

Hi,
I am looking for a way to mask user-input in my perl scripts. I have perl script which ask's for a user password. I am wondering if something like this is possible in perl.

C:\myscript.prl Password: ********

or maybe the password is not echoed back to the user at all. I am looking for a solution that will work both on Windows, and UNIX. I know in UNIX something like this is possible.

print "Password : "; system ("stty -echo"); my $password = <STDIN> system ("stty echo");
But for windows I was not able to find a way to turn command echoing off in the command prompt.

Replies are listed 'Best First'.
Re: Password masking
by Kanji (Parson) on May 02, 2002 at 17:46 UTC

    Have you looked at Term::ReadKey's (alt./ppd) ReadMode?

    ReadMode( noecho => STDIN ); my $password = <STDIN>; ReadMode( restore => STDIN );

    Things get a little trickier if you want asterisks to show up inplace of input, but nothing ReadKey, a loop, and print '*' can't handle...

        --k.


Re: Password masking
by djw (Vicar) on May 02, 2002 at 17:49 UTC
    You may want to take at a look at Term::ReadKey from CPAN. From the docs:

    The terminal mode function is controlled by the "ReadMode" function, which takes a single numeric argument, and an optional filehandle. This argument should be one of the following:
    • 0: (Reset) Restore original settings.
    • 1: (Cooked) Change to what is commonly the default mode, echo on, buffered, signals enabled, Xon/Xoff possibly enabled, and 8-bit mode possibly disabled.
    • 2: (Cooked-Invisible) Same as 1, just with echo off. Nice for reading passwords.
    • 3: (CBreak) Echo off, unbuffered, signals enabled, Xon/Xoff possibly enabled, and 8-bit mode possibly enabled.
    • 4: (Raw) Echo off, unbuffered, signals disabled, Xon/Xoff disabled, and 8-bit mode possibly disabled.
    • 5: (Really-Raw) Echo off, unbuffered, signals disabled, Xon/Xoff disabled, 8-bit mode enabled if parity permits, and CR to CR/LF translation turned off.

    I didn't try it out, but it looks to be exactly what you need.

    djw

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://163606]
Approved by VSarkiss
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (6)
As of 2024-04-19 12:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found