Don't use an invalid salt when encrypting password

This commit is contained in:
Jamie Cameron
2009-11-16 21:41:40 -08:00
parent 2d1396ddbb
commit b4abb02136
2 changed files with 14 additions and 1 deletions

View File

@@ -24,12 +24,17 @@ return undef;
sub encrypt_md5
{
local $passwd = $_[0];
local $salt = $_[1];
local $magic = '$1$';
local $salt = $_[1] || substr(time(), -8);
if ($salt =~ /^\$1\$([^\$]+)/) {
# Extract actual salt from already encrypted password
$salt = $1;
}
if ($salt !~ /^[a-z0-9\/]{8}$/i) {
# Non-MD5 salt
$salt = undef;
}
$salt ||= substr(time(), -8);
# Use built-in crypt support for MD5, if we can
if (&unix_crypt_supports_md5()) {
@@ -162,6 +167,10 @@ sub encrypt_blowfish
local ($passwd, $salt) = @_;
local ($plain, $base64);
eval "use Crypt::Eksblowfish::Bcrypt";
if ($salt !~ /^\$2a\$/) {
# Invalid salt for Blowfish
$salt = undef;
}
if (!$salt) {
# Generate a 22-character base-64 format salt
&seed_random();

View File

@@ -1796,6 +1796,10 @@ elsif ($format == 2) {
}
else {
# Just do old-style crypt() DES encryption
if ($salt !~ /^[a-z0-9]{2}/i) {
# Un-usable non-DES salt
$salt = undef;
}
$salt ||= chr(int(rand(26))+65) . chr(int(rand(26))+65);
return &unix_crypt($pass, $salt);
}