gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 09:13:15 28 4
gpt4 key购买 nike

我有这个加密密码的脚本,但我不知道如何反转和解密它。这可能是一个非常简单的答案,但我不明白该怎么做。

#!/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 上的计算成本很高)以获得该知识。

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

关于perl - 如何解密 bcrypt 存储的哈希,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18084595/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com