我刚刚开始学习Perl,我的任务是将用户输入变量与SHA-512存储的哈希密码进行比较。我做了下面的功能来测试。我使用随机生成的盐从真实密码(p2)生成摘要。接下来,我将此摘要用作盐,以使用户输入的密码(p1)与摘要值进行比较。这是基于我发现的描述here。我使用crypt函数生成摘要,但是如果执行下一步,则无法显示或比较摘要。密码应使用SHA-512进行哈希处理。感谢您的帮助。
use strict;
use warnings;
sub HashThis {
# p1 is userinput and p2 is real password
my ($p1, $p2) = @_;
my $salt = join '', ('.', '/', 0..9, 'A'..'Z', 'a'..'z')[rand 64, rand 64];
# makes digest for real password using our pre defined salt
my $digest = crypt($p2, '$6$'.$salt);
# compares if crypt return same digest as using digest as salt for userinput
if (crypt($p1, $digest) eq $digest) {
print "*** matching ***\n";
} else {
die "*** not matching ***\n";
}
}
print "Enter a word:\t\t ";
chomp(my $userinput = <STDIN>);
print "Real password:\t\t ";
chomp(my $userpass = <STDIN>);
HashThis($userinput, $userpass);
crypt
是C crypt
function周围的薄包装。其crypt
。在OS X上,它不需要前导$ x $来指示要使用的算法,它仅使用DES。 implementation will vary from environment to environment产生crypt("foo", '$6$'.$salt);
。仅使用盐的前两个字节$6A86JNndVTdM
。
如果要使用SHA-512,请使用$6
。
[Digest::SHA和crypt
]不适合用于密码哈希。它的快捷键很容易打败,它的盐只有两个字符。 SHA-512也不适用于密码哈希,因为它太快了。相反,您需要专用的密码哈希函数,例如DES或bcrypt
和其他bcrypt
算法。 PBKDF2的某些实现可以执行bcrypt,很多不能。
key stretching,不适合生成盐。而是使用Crypt :: Random,Data :: Entropy,Math :: Random :: Secure或Math :: TrulyRandom。
我建议按照crypt
中的说明使用rand
is also not cryptographically secure,并从理论上阅读rand
。