Created
April 22, 2019 10:41
-
-
Save josnidhin/88274124c3f2983f8f26eb8d5a7c11f7 to your computer and use it in GitHub Desktop.
An example on how to use bcrypt to securely hash passwords in Perl
This file contains hidden or 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
use Crypt::Eksblowfish::Bcrypt qw(bcrypt en_base64); | |
use Data::Entropy::Algorithms qw(rand_bits); | |
my $bcrypt_cost = 12; | |
sub encrypt_password { | |
my $password = shift; | |
my $salt = en_base64(rand_bits(16*8)); | |
my $settings = '$2a'.'$'.$bcrypt_cost.'$'.$salt; | |
return bcrypt($password, $settings); | |
} | |
sub check_password { | |
my $password = shift; | |
my $hash = shift; | |
if ($hash eq bcrypt($password, $hash)) { | |
return 1; | |
} else { | |
return 0; | |
} | |
} | |
my $hash = encrypt_password('test'); | |
print check_password('hello', $hash); | |
print check_password('test', $hash); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment