Perl Digest Bcrypt,生成适当的散列

Perl Digest Bcrypt, generating a proper hash

我编写了一个生成 Bcrypt 散列的测试程序。此哈希稍后需要由 PHP 后端验证。

这是我的 perl 代码:

use Digest;
#use Data::Entropy::Algorithms qw(rand_bits);


#my $bcrypt = Digest->new('Bcrypt', cost=>10, salt=>rand_bits(16*8));
my $bcrypt = Digest->new('Bcrypt', cost=>10, salt=>'1111111111111111');
my $settings = $bcrypt->settings(); # save for later checks.
my $pass_hash = $bcrypt->add('bob')->b64digest;
print $settings.$pass_hash."\n";

这会打印

a$KRCvKRCvKRCvKRCvKRCvKOoFxCE1d/OZTKQqhet3bKOq6ZVIACXBU

如果我使用 https://bcrypt-generator.com

等在线 bcrypt 工具,这不会验证为正确的哈希

谁能指出错误?谢谢

解决了问题。我必须使用 bcrypt_b64digest 而不是 b64digest。我希望 perl 文档更清楚,其中需要使用一个,以便其他 bcrypt 实现可以 "get it"。

my $pass_hash = $bcrypt->add('bob')->bcrypt_b64digest;

来自https://metacpan.org/pod/Digest::Bcrypt#bcrypt_b64digest

Same as "digest", but will return the digest base64 encoded using the alphabet that is commonly used with bcrypt. The length of the returned string will be 31 and will only contain characters from the ranges '0'..'9', 'A'..'Z', 'a'..'z', '+', and '.'

The base64 encoded string returned is not padded to be a multiple of 4 bytes long. Note: This is bcrypt's own non-standard base64 alphabet, It is not compatible with the standard MIME base64 encoding.