perl - 使用内置 crypt 验证哈希密码与用户输入

标签 perl crypt sha512

我正在尝试找出如何使用 Crypt 函数来验证存储的哈希密码与用户输入的数据。

我使用下面的代码使用随机生成的盐生成密码摘要。在下一步中,我使用之前生成的摘要作为 crypt 函数和用户输入的盐。基于 this 中的信息链接,如果 crypt 函数的输出与我们之前生成的摘要相同,那么我们就可以开始了。

CheckThis 函数中的 crypt 函数使用之前生成的摘要生成不同的输出,这会导致问题。我做错了什么?

这是我的代码:

use strict;
use warnings;

sub HashThis {
    # To generate random salt
    my @temp = (0..9, 'A'..'Z', 'a'..'z');
    my $salt;
    $salt .= $temp[rand @temp] for 1..16;
    print "\nSalt is:\t\t",$salt,"\n";

    # makes digest for real password using salt
    my $digest = crypt(@_, '$6$'.$salt);

    return ($digest);
}

sub CheckThis {
    # compares if crypt return same digest as using digest as salt for userinput
    my $result;
    my ($ui, $digest) = @_;

    if (crypt($ui, $digest) eq $digest) {
        $result = "matching";
    } else {
        $result = "not matching";
    }    
    return ($result);
}

system "stty -echo";
print "\nReal password:\t\t ";
chomp(my $userpass = <STDIN>);
print "\n";
system "stty echo";

my $digest = HashThis($userpass);
print "\nDigest is:\t\t",$digest,"\n";

system "stty -echo";
print "\nTest password:\t\t ";
chomp(my $userinput = <STDIN>);
print "\n";
system "stty echo";

my $final_result = CheckThis($userinput, $digest);
print "\n",$final_result,"\n\n";

最佳答案

更改:

my $digest = crypt(@_, '$6$'.$salt);

至:

my $digest = crypt($_[0], '$6$'.$salt);

我的猜测是 crypt 期望传递 2 个标量值,并且它在标量上下文中评估 @_ 数组变量,由于您传递了一个标量值,因此该变量为 1 HashThis sub 的值。

关于perl - 使用内置 crypt 验证哈希密码与用户输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62204842/

相关文章:

C# 模拟 php crypt

perl - apache2-Ubuntu 中的 Bugzilla 安装问题

perl - 如何获得最低匹配的 captureno?

perl - Perl 中的排序函数

Perl DATA 文件句柄在读取时为空

php - crypt() 函数的最大输出

php异步地穴

php - SHA512 登录不正确从数据库检索密码

javascript - 在提交之前在 javascript 中加密密码是否有意义?

powershell - 文件的 PS SHA512 哈希,输出为 base 64 编码字符串