Skip to content

Instantly share code, notes, and snippets.

@dallaylaen
Created July 2, 2014 12:33
Show Gist options
  • Save dallaylaen/cedd604914c6d7bf3c9c to your computer and use it in GitHub Desktop.
Save dallaylaen/cedd604914c6d7bf3c9c to your computer and use it in GitHub Desktop.
Learn you new, shiny, random password the hard way
#!/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