加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

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

发布时间:2020-12-15 21:38:47 所属栏目:大数据 来源:网络整理
导读:我有这个脚本加密密码,但我不知道如何扭转它并解密.这可能是一个非常简单的答案,但我不明白如何做到这一点. #!/usr/bin/perluse Crypt::Eksblowfish::Bcrypt;use Crypt::Random;$password = 'bigtest';$encrypted = encrypt_password($password);print "$pas
我有这个脚本加密密码,但我不知道如何扭转它并解密.这可能是一个非常简单的答案,但我不明白如何做到这一点.
#!/usr/bin/perl
use Crypt::Eksblowfish::Bcrypt;
use Crypt::Random;

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

print "Yes the password is $passwordn" if check_password($password,$encrypted);
print "No the password is not smalltestn" 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计算上是昂贵的)来获得这些知识.

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读