can anyone give me a sample code for letting a user to enter a password two times and compare them and print a text if they are correct or not, like when we create a new user. Thanks in advance...
Asked
Active
Viewed 309 times
2
-
If your code needs to store the password so that user-inputted passwords can be confirmed later, please use [a cryptographic hash function](http://en.wikipedia.org/wiki/Cryptographic_hash_function), like say, [SHA-2](http://en.wikipedia.org/wiki/SHA-2). – In silico Dec 10 '10 at 04:48
-
@hanmyo - I am a beginner.. this is required for the workshop of one of my subjects. I know doing coding in java and php but do not know how to use modules actually.. – user403295 Dec 10 '10 at 05:21
3 Answers
2
From perldoc -f crypt,
$pwd = (getpwuid($<))[1];
system "stty -echo";
print "Password: ";
chomp($word = <STDIN>);
print "\n";
system "stty echo";
if (crypt($word, $pwd) ne $pwd) {
die "Sorry...\n";
} else {
print "ok\n";
}
Modify to suit your needs.

ephemient
- 198,619
- 38
- 280
- 391
-
yes this is good and i think i have seen before.. i will try to change, i am a beginner..thanks. – user403295 Dec 10 '10 at 05:19
-
this isn't good, this is code to check a password against a previously crypted password, not to check two entries for equality – ysth Dec 10 '10 at 06:33
-
Yes, but it shows how to read a password at the console, and I figured I'd just copy the whole snippet with attribution. Seems to have been enough to get OP going. – ephemient Dec 10 '10 at 06:35
0
here is the answer based on ephemient's hint.. I dont need the first line that i removed and in if case ne suppose to be eq, probably means equal:)it looks it works..
system "stty -echo";
print "Password: ";
chomp($word = <STDIN>);
print "Password again: ";
chomp($pwd = <STDIN>);
print "\n";
system "stty echo";
if (crypt($word, $pwd) eq $pwd) {
die "Sorry...\n";
} else {
print "ok\n";
}

user403295
- 818
- 3
- 15
- 19
0
crypt is a red herring.
system "stty -echo";
print "Password: ";
chomp(my $password = <STDIN>);
print "\nPassword again: ";
chomp(my $check_again = <STDIN>);
print "\n";
system "stty echo";
if ($password ne $check_again) {
die "Sorry...\n";
} else {
print "ok\n";
}

ysth
- 96,171
- 6
- 121
- 214
-
-
thanks for the answer. I modified the ephemient's sample as required. The answer is below.and it is very similar to yours except if case. if you use "ne" between $password and $check_again, it drops into "Sorry" part although the passwords are the same. So I used 'eq' instead of 'ne'. And it looks it works. I will try yours too.. I havent run it yet. Then I will also check your answer.. Thanks again..Regards.. – user403295 Dec 11 '10 at 03:04
-
@user403295: you *don't* want to use crypt to check if two entered strings are the same. Really. you want to just use ne. if my code didn't work for you, you didn't copy it exactly. Try again? – ysth Dec 12 '10 at 06:57