Benutze ich die crypt () - Funktion von PHP richtig?

Habe ich mit PHP crypt() als eine Möglichkeit zum speichern und überprüfen der Passwörter in meiner Datenbank. Ich benutze hashing für andere Dinge, aber crypt() für Passwörter. Die Dokumentation ist nicht so gut und es scheint eine Menge Diskussion. Ich bin mit blowfish und zwei Salzen zu crypt ein Passwort ein und speichern es in die Datenbank. Bevor ich speichert den salt und das verschlüsselte Passwort (wie ein gesalzener hash), merkte aber an seine redundant, da das Salz ist Teil der verschlüsselten Passwort-string.

Ich bin ein wenig verwirrt, wie rainbow-table-Angriffe funktionieren würde crypt() jedenfalls schaut das korrekt aus Sicht der Sicherheit. Ich benutze eine zweite Salz-zum Anhängen an das Passwort zur Erhöhung der Entropie kurze Passwörter, wahrscheinlich übertrieben, aber warum nicht?

function crypt_password($password) {
if ($password) {
    //find the longest valid salt allowed by server
    $max_salt = CRYPT_SALT_LENGTH;

    //blowfish hashing with a salt as follows: "$2a$", a two digit cost parameter, "$", and 22 base 64
    $blowfish = '$2a$10$';

    //get the longest salt, could set to 22 crypt ignores extra data
    $salt = get_salt ( $max_salt );

    //get a second salt to strengthen password
    $salt2 = get_salt ( 30 ); //set to whatever


    //append salt2 data to the password, and crypt using salt, results in a 60 char output
    $crypt_pass = crypt ( $password . $salt2, $blowfish . $salt );

    //insert crypt pass along with salt2 into database.
    $sql = "insert into database....";

    return true;
    }
}  


function get_salt($length) {
$options = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789./';

$salt = '';

for($i = 0; $i <= $length; $i ++) {
    $options = str_shuffle ( $options );
    $salt .= $options [rand ( 0, 63 )];
}
return $salt;
}

function verify_password($input_password)
{
if($input_password)
{
    //get stored crypt pass,and salt2 from the database
    $stored_password = 'somethingfromdatabase';
    $stored_salt2 = 'somethingelsefromdatabase';

    //compare the crypt of input+stored_salt2 to the stored crypt password
    if (crypt($input_password . $stored_salt2, $stored_password) == $stored_password) {
        //authenticated
        return true;
    }
    else return false;
}
else return false;
}

InformationsquelleAutor der Frage Brian | 2010-09-29

Schreibe einen Kommentar