Read a password from keyboard...

D

Dominic Dupuis

Bonjour,

Does anybody have a way to read a password, from a user, and only
display "****" for each character read?!?

Thanks for any idea! :)

PS: I want to do that (via a Perl script) on Linux and Windows.
 
S

Sherm Pendley

Dominic said:
Does anybody have a way to read a password, from a user, and only
display "****" for each character read?!?

That Question is Frequently Asked, so appears in the FAQ:

perldoc -q password

sherm--
 
J

Jürgen Exner

Dominic said:
Does anybody have a way to read a password, from a user, and only
display "****" for each character read?!?

Which part of your question is not answered in "perldoc -q password":

How do I ask the user for a password?


jue
 
M

MrReallyVeryNice

Dominic said:
Bonjour,

Does anybody have a way to read a password, from a user, and only
display "****" for each character read?!?

Thanks for any idea! :)

PS: I want to do that (via a Perl script) on Linux and Windows.

Bonjour,

Maybe the following sample will give you some ideas. You still have to
test it on Linux and Windows because I did not do that for you. :)
Listen to gurus who might to review/critique this quick hack. The
script is by no mean fully tested and foolproof.

Let us know if works for you!
MrReallyVeryNice

use strict;
use warnings;
use Term::ReadKey;

my $number_attempt=0;
my $password1 = hide_password($number_attempt);
my $max_number_attempt=3;
my $password2 = hide_password($number_attempt);
while ($number_attempt<$max_number_attempt+1)
{
if ($password1 ne $password2)
{
$password2 = hide_password($number_attempt);
}
else
{
last;
}
}
if ($password1 ne $password2)
{
print "\nYou failed to confirm your password\n";
}
else
{
# you most likely want to remove the print statement below and
# do whatever processing you need to do with the confirmed password
print "\nYour password was confirmed\n";
}


sub hide_password {
$|=1;
my $password = '';
my $key;
if ($_[0] == 0)
{
print "Enter password:\n";
}
else
{
print "\nConfirm password:\n";
}
while(1)
{
while(not defined($key = ReadKey(-1))) {};
if(ord($key) != 13)
{
if(ord($key) == 8)
{
print "\b\0\b";
chop($password);
}
else
{
$password .= $key;
print "*";
}
}
else {last;}
}
$|=0;
$number_attempt++;
return $password;
}

__END__
 
J

Joe Smith

Sherm said:
That Question is Frequently Asked, so appears in the FAQ:

perldoc -q password

But that does not do exactly what the poster asked.

I interpreted it as "How can I read a password a character at a time
so that asterisks can be printed as the password is being typed."

Term::ReadKey has ReadMode('noecho'), but not ReadMode('noecho+cbreak').
It is ReadLine(0) but not ReadLineWithAsterisksPrinted(0).

Of course if the displaying of asterisks is not really needed,
then the FAQ answer is sufficient.
-Joe
 
J

Joe Smith

Joe said:
I interpreted it as "How can I read a password a character at a time
so that asterisks can be printed as the password is being typed."

Ignore my previous answer.
This can give the OP something to think about:

perl -MTerm::ReadKey -e
'$|=ReadMode('raw');print"PW:";for(;;){$_=ReadKey 0;last if$_
eq"\n";$p.=$_;printf"%02x*",ord}print "\n$p\n"'

-Joe
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,164
Messages
2,570,898
Members
47,440
Latest member
YoungBorel

Latest Threads

Top