【问题标题】:Verifying hashed password vs user input with crypt builtin使用 crypt 内置验证散列密码与用户输入
【发布时间】:2020-06-04 21:58:10
【问题描述】:

我正在尝试找出如何使用 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";

【问题讨论】:

    标签: perl crypt sha512


    【解决方案1】:

    变化:

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

    到:

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

    我的猜测是 crypt 期望传递 2 个标量值,它在标量上下文中评估 @_ 数组变量,这是 1,因为您将单个值传递给 HashThis sub

    【讨论】:

    • 看起来是一个合理的猜测——perl -E 'say prototype("CORE::crypt")' 返回$$
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-27
    • 2018-03-01
    • 2013-05-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多