Created
July 2, 2014 12:33
-
-
Save dallaylaen/cedd604914c6d7bf3c9c to your computer and use it in GitHub Desktop.
Learn you new, shiny, random password the hard way
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/perl -w | |
use strict; | |
use Term::ReadKey; | |
END{ ReadMode(0) }; | |
ReadMode('noecho'); | |
$|=1; | |
# read password twice | |
my $realpw = readpass(); | |
$realpw eq readpass() or die "Password doesn't match"; | |
my $len = readpass("Enter password length"); | |
$len == length $realpw or die "Password length doesn't match"; | |
# now password is probably what user really watned. Learn... | |
my $err = 0; | |
my $n = 12; | |
my $thresh = 1; | |
for (1..$n) { | |
my $pw = readpass(); | |
if ($pw ne $realpw) { | |
$err++; | |
print "Oops!\n"; | |
}; | |
}; | |
if ($err <= $thresh) { | |
print "Password remembered! (well, likely)\n"; | |
} else { | |
print "Too messy. Sorry, but you'd better try again\n"; | |
} | |
exit $err; | |
sub readpass { | |
my $msg = shift || "Enter your password"; | |
print "$msg: "; | |
my $pw = <STDIN>; | |
print "\n"; | |
defined $pw or die "Stopped abruptly\n"; | |
chomp $pw; | |
return $pw; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment