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

Hello, I'm a newbie and have a simple question about passwords. My thought is to ask the user for their password to use later on in the script (getting the pw via STDIN) but I was wondering how to keep the password from being seen? Anyone have any suggestions. By the way, I'm using this on a Win32 script. Thanks

20031115 Edit by BazB: Changed title from 'simple password question'

  • Comment on Reading password from STDIN without echoing

Replies are listed 'Best First'.
Re: Reading password from STDIN without echoing
by AcidHawk (Vicar) on Nov 14, 2003 at 18:59 UTC

    Please NOTE! this is a NOT recommended method of hiding passwords, but to answer your question, keep the password from being seen?

    Look at Term::ReadKey

    #! /use/bin/perl use strict; use Term::ReadKey; ReadMode( "noecho"); print "Enter pwd please :"; chomp (my $pwd = <>); ReadMode ("original") ; print "\nYou typed $pwd!\n";

    Update: This has been tested on a Win32 platform.

    -----
    Of all the things I've lost in my life, its my mind I miss the most.
      So you mention that the above is not the recommoned method of hiding passwords. Can I ask what is? By the way thanks for answering my question.
Re: Reading password from STDIN without echoing
by hardburn (Abbot) on Nov 14, 2003 at 19:00 UTC

    Try Term::ReadPassword, though I'm not sure if it works in a cross-platform manner. If not, you might be able to combine it with Win32::Console::ANSI.

    ----
    I wanted to explore how Perl's closures can be manipulated, and ended up creating an object system by accident.
    -- Schemer

    : () { :|:& };:

    Note: All code is untested, unless otherwise stated

      No, Term::ReadKey is as portable as it gets.
      E:\>perldoc -q password Found in C:\Perl\lib\pod\perlfaq8.pod How do I ask the user for a password? (This question has nothing to do with the web. See a different FAQ + for that.) There's an example of this in "crypt" in perlfunc). First, you put + the terminal into "no echo" mode, then just read the password normally +. You may do this with an old-style ioctl() function, POSIX terminal con +trol (see POSIX or its documentation the Camel Book), or a call to the +stty program, with varying degrees of portability. You can also do this for most systems using the Term::ReadKey modu +le from CPAN, which is easier to use and in theory more portable. use Term::ReadKey; ReadMode('noecho'); $password = ReadLine(0);

      MJD says "you can't just make shit up and expect the computer to know what you mean, retardo!"
      I run a Win32 PPM repository for perl 5.6.x and 5.8.x -- I take requests (README).
      ** The third rule of perl club is a statement of fact: pod is sexy.

Re: Reading password from STDIN without echoing
by pg (Canon) on Nov 14, 2003 at 20:16 UTC

    If you don't insist STDIN, and open to GUI solution, I stripped piece of my old code, and here it is: (you don't need to port your entire application to GUI, just use this to get password. It can be easily wrapped in a function.)

    use Tk; use Tk::DialogBox; use strict; use warnings; my $mw = MainWindow->new(title => "Password Demo"); my $login_first_try = 1; my $id; my $passwd; while (1) { my $dialog = $mw->DialogBox(-buttons => ["OK", "Cancel"], -title = +> "Login"); my $instruction; if ($login_first_try) { $instruction = "Please provide id/passwd: "; $login_first_try = 0; } else { $instruction = "Failed to login, please try again: " } $dialog->add("Label", anchor => "w", text => $instruction)->pack(f +ill => "x"); my $id_text = $dialog->add("Entry", width => 30)->pack(); my $passwd_text = $dialog->add("Entry", show => "*", width => 30)- +>pack(); $id_text->focus(); if ($dialog->Show() eq "OK") { print "OK pressed, validate here, if okay last and quit the wh +ile loop" #............. } else { exit; } }