Skip to content

Instantly share code, notes, and snippets.

@lifeofguenter
Created April 27, 2014 18:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lifeofguenter/11351790 to your computer and use it in GitHub Desktop.
Save lifeofguenter/11351790 to your computer and use it in GitHub Desktop.
A simplified "API" tp "safely" hash and verify passwords in Perl
################################################################################
# A simplified "API" to "safely" hash and verify passwords #
# #
# Author: Günter Grodotzki <guenter@perlhipster.com> #
# License: http://www.gnu.org/licenses/gpl-2.0.txt #
# Version: 20140425 #
################################################################################
use Authen::Passphrase::BlowfishCrypt;
sub password_hash {
my ($password, $cost) = @_;
if (!defined $cost || $cost =~ /\D/ || $cost < 4 || $cost > 31) {
$cost = 14;
}
my $ppr = Authen::Passphrase::BlowfishCrypt->new(
cost => $cost,
salt_random => 1,
passphrase => $password
);
return $ppr->as_rfc2307;
}
sub password_verify {
my ($password, $hash) = @_;
my $ppr = Authen::Passphrase::BlowfishCrypt->from_rfc2307($hash);
return $ppr->match($password);
}
1;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment