Skip to content

Instantly share code, notes, and snippets.

@josnidhin
Created April 22, 2019 10:41
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 josnidhin/88274124c3f2983f8f26eb8d5a7c11f7 to your computer and use it in GitHub Desktop.
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
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