Come posso inserire una password utilizzando Perl e sostituire i caratteri con '*'?
Domanda
Ho uno script Perl che richiede all'utente di inserire una password. Come Posso solo echeggiare '*' al posto del personaggio che l'utente, come che tipo è?
Sto usando Windows XP / Vista.
Soluzione
Si può giocare con Term :: ReadKey. Ecco un esempio molto semplice, con un po 'di rilevamento per tornare indietro e cancellare chiave. Ho provato su Mac OS X 10.5, ma in base alla ReadKey manuale esso dovrebbe funzionare sotto Windows. Il manuale indica che sotto Windows utilizzando non bloccante letture (ReadKey(-1)
) avrà esito negativo. È per questo che sto usando ReadKey (0) che è fondamentalmente getc
(più su getc nel libc manuale ).
#!/usr/bin/perl
use strict;
use warnings;
use Term::ReadKey;
my $key = 0;
my $password = "";
print "\nPlease input your password: ";
# Start reading the keys
ReadMode(4); #Disable the control keys
while(ord($key = ReadKey(0)) != 10)
# This will continue until the Enter key is pressed (decimal value of 10)
{
# For all value of ord($key) see http://www.asciitable.com/
if(ord($key) == 127 || ord($key) == 8) {
# DEL/Backspace was pressed
#1. Remove the last char from the password
chop($password);
#2 move the cursor back by one, print a blank character, move the cursor back by one
print "\b \b";
} elsif(ord($key) < 32) {
# Do nothing with these control characters
} else {
$password = $password.$key;
print "*(".ord($key).")";
}
}
ReadMode(0); #Reset the terminal once we are done
print "\n\nYour super secret password is: $password\n";
Altri suggerimenti
In passato ho usato IO :: Prompt per questo.
use IO::Prompt;
my $password = prompt('Password:', -e => '*');
print "$password\n";
Se non si desidera utilizzare tutti i pacchetti ... Solo per UNIX
system('stty','-echo');
chop($password=<STDIN>);
system('stty','echo');
Si dovrebbe dare un'occhiata a entrambi i Term :: ReadKey o Win32 :: Console . È possibile utilizzare questi moduli per leggere le combinazioni di tasti singoli ed emettono '*' o whathever.
Sulla base del programma di Pierr-Luc, appena aggiunto un certo controllo sulle backslash. Con questo, non si può continuare a premere backslash per sempre:
sub passwordDisplay() {
my $password = "";
# Start reading the keys
ReadMode(4); #Disable the control keys
my $count = 0;
while(ord($key = ReadKey(0)) != 10) {
# This will continue until the Enter key is pressed (decimal value of 10)
# For all value of ord($key) see http://www.asciitable.com/
if(ord($key) == 127 || ord($key) == 8) {
# DEL/Backspace was pressed
if ($count > 0) {
$count--;
#1. Remove the last char from the password
chop($password);
#2 move the cursor back by one, print a blank character, move the cursor back by one
print "\b \b";
}
}
elsif(ord($key) >= 32) {
$count++;
$password = $password.$key;
print "*";
}
}
ReadMode(0); #Reset the terminal once we are done
return $password;
}
utilizzando il programma di Pierr-Luc
# Start reading the keys
ReadMode(4); #Disable the control keys
while(ord($key = ReadKey(0)) != '13' )
# This will continue until the Enter key is pressed (decimal value of 10)
{
# For all value of ord($key) see http://www.asciitable.com/
if(ord($key) == 127 || ord($key) == 8 && (length($password) > 0)) {
# DEL/Backspace was pressed
#1. Remove the last char from the password
chop($password);
#2 move the cursor back by one, print a blank character, move the cursor back by one
print "\b \b";
} elsif(ord($key) > 32) {
$password = $password.$key;
print "*";
}
}
ReadMode(0); #Reset the terminal once we are done
Hai provato memorizzare la stringa (in modo che il programma può ancora leggere) e scoprire la sua lunghezza quindi creare una stringa della stessa lunghezza, ma usa solo '*'?