perl – 如何解密bcrypt存储的哈希

我有这个脚本加密密码,但我不知道如何扭转它并解密.这可能是一个非常简单的答案,但我不明白如何做到这一点.
#!/usr/bin/perl
use Crypt::Eksblowfish::Bcrypt;
use Crypt::Random;

$password = 'bigtest';
$encrypted = encrypt_password($password);
print "$password is encrypted as $encrypted\n";

print "Yes the password is $password\n" if check_password($password,$encrypted);
print "No the password is not smalltest\n" if !check_password('smalltest',$encrypted);

# Encrypt a password 
sub encrypt_password {
    my $password = shift;

    # Generate a salt if one is not passed
    my $salt = shift || salt(); 

    # Set the cost to 8 and append a NUL
    my $settings = '$2a$08$'.$salt;

    # Encrypt it
    return Crypt::Eksblowfish::Bcrypt::bcrypt($password,$settings);
}

# Check if the passwords match
sub check_password {
    my ($plain_password,$hashed_password) = @_;

    # Regex to extract the salt
    if ($hashed_password =~ m!^\$2a\$\d{2}\$([A-Za-z0-9+\\.]{22})!) {
        return encrypt_password($plain_password,$1) eq $hashed_password;
    } else {
        return 0;
    }
}

# Return a random salt
sub salt {
    return Crypt::Eksblowfish::Bcrypt::en_base64(Crypt::Random::makerandom_octet(Length=>16));
}

解决方法

你在洗澡,不加密!

有什么不同?

区别在于散列是单向函数,其中加密是双向函数.

那么,你如何确定密码是正确的?

因此,当用户提交密码时,不会对存储的哈希进行解密,而是对用户输入执行相同的bcrypt操作,并比较哈希值.如果它们相同,则接受身份验证.

你应该加密密码吗?

你现在在做什么 – 哈希密码是正确的.如果您只是加密密码,则您的应用程序的安全性可能会导致恶意用户轻松学习所有用户密码.如果您的哈希(或更好的,salt and hash)密码,用户需要破解密码(这在bcrypt计算上是昂贵的)来获得这些知识.

由于您的用户可能会在不止一个地方使用他们的密码,这将有助于保护他们.

相关文章

1. 如何去重 #!/usr/bin/perl use strict; my %hash; while(...
最近写了一个perl脚本,实现的功能是将表格中其中两列的数据...
表的数据字典格式如下:如果手动写MySQL建表语句,确认麻烦,...
巡检类工作经常会出具日报,最近在原有日报的基础上又新增了...
在实际生产环境中,常常需要从后台日志中截取报文,报文的形...
最近写的一个perl程序,通过关键词匹配统计其出现的频率,让...