15.10. Reading PasswordsProblemYou want to read input from the keyboard without the keystrokes being echoed on the screen. For instance, you want to read passwords as passwd does, i.e. without displaying the user's password. Solution
Use the CPAN module Term::ReadKey, set the input mode to use Term::ReadKey; ReadMode('noecho'); $password = ReadLine(0); Discussion
Example 15.3
shows how to verify a user's password. If your system uses shadow passwords, only the superuser can get the encrypted form of the password with Example 15.3: checkuser#!/usr/bin/perl -w # checkuser - demonstrates reading and checking a user's password use Term::ReadKey; print "Enter your password: "; ReadMode 'noecho'; $password = ReadLine 0; chomp $password; ReadMode 'normal'; print "\n"; ($username, $encrypted) = ( getpwuid $< )[0,1]; if (crypt($password, $encrypted) ne $encrypted) { die "You are not $username\n"; } else { print "Welcome, $username\n"; } See Also
The documentation for the Term::ReadKey module from CPAN; the
Copyright © 2001 O'Reilly & Associates. All rights reserved. |
|