كيف يمكنني إدخال كلمة المرور باستخدام بيرل واستبدال الأحرف '*'؟
سؤال
ولدي سيناريو بيرل الذي يطلب من المستخدم إدخال كلمة مرور. كيف يمكنني صدى فقط '*' بدلا من الحرف الذي يكتب المستخدم، كما اكتبه؟
وأنا باستخدام ويندوز XP / فيستا.
المحلول
ويمكنك ان تلعب مع الأجل :: ReadKey. هنا مثال بسيط جدا، مع بعض الكشف عن مسافة للخلف وحذف المفتاح. لقد اختبرت ذلك على نظام التشغيل Mac OS X 10.5 ولكن وفقا ل ReadKey دليل في هذا الشأن يجب أن تعمل تحت ويندوز. و دليل يشير إلى أنه في ظل ويندوز تستخدم غير مؤمن يقرأ (ReadKey(-1)
) ستفشل. لهذا السبب أنا باستخدام ReadKey (0) الذين في الاساس getc
(أكثر على getc في في دليل LIBC ).
#!/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";
نصائح أخرى
في الماضي كنت قد استخدمت IO :: موجه للحصول على ذلك.
use IO::Prompt;
my $password = prompt('Password:', -e => '*');
print "$password\n";
إذا كنت لا تريد استخدام أي حزم ... فقط ل UNIX
system('stty','-echo');
chop($password=<STDIN>);
system('stty','echo');
ويجب أن نلقي نظرة على إما الأجل :: ReadKey أو <أ href ل = "http://search.cpan.org/perldoc؟Win32::Console" يختلط = "noreferrer"> Win32 و:: وحدة التحكم . يمكنك استخدام هذه الوحدات لقراءة ضربات لوحة المفاتيح واحدة وتطلق '*' أو whathever.
وبناء على برنامج Pierr لوك ل، واضاف عادل بعض السيطرة على خطوط مائلة عكسية. مع ذلك، لا يمكن أن تبقي الضغط مائل إلى الأبد:
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;
}
وباستخدام برنامج Pierr لوك ل
# 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
هل حاولت تخزين سلسلة (حتى أن البرنامج لا يزال قراءتها) ومعرفة طوله قم بإنشاء سلسلة من نفس الطول، ولكن استخدام فقط '*؟