Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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