How can I store my users' passwords safely?

后端 未结 6 1788
长发绾君心
长发绾君心 2020-11-21 23:37

How much more safe is this than plain MD5? I\'ve just started looking into password security. I\'m pretty new to PHP.

$salt = \'csdnfgksdgojnmfnb\';

$passwo         


        
6条回答
  •  醉梦人生
    2020-11-21 23:51

    The easiest way to get your password storage scheme secure is by using a standard library.

    Because security tends to be a lot more complicated and with more invisible screw up possibilities than most programmers could tackle alone, using a standard library is almost always easiest and most secure (if not the only) available option.


    The new PHP password API (5.5.0+)

    If you are using PHP version 5.5.0 or newer, you can use the new simplified password hashing API

    Example of code using PHP's password API:

     12]);
    
    // $hash would be the $hash (above) stored in your database for this user
    $checked = password_verify($_POST['password'], $hash);
    if ($checked) {
        echo 'password correct';
    } else {
        echo 'wrong credentials';
    }
    

    (In case you are still using legacy 5.3.7 or newer you can install ircmaxell/password_compat to have access to the build-in functions)


    Improving upon salted hashes: add pepper

    If you want extra security, the security folks now (2017) recommend adding a 'pepper' to the (automatically) salted password hashes.

    There is a simple, drop in class that securely implements this pattern, I recommend: Netsilik/PepperedPasswords (github).
    It comes with a MIT License, so you can use it however you want, even in proprietary projects.

    Example of code using Netsilik/PepperedPasswords:

    hash($_POST['password']);
    
    // $hash would be the $hash (above) stored in your database for this user
    $checked = $hasher->verify($_POST['password'], $hash);
    if ($checked) {
        echo 'password correct';
    } else {
        echo 'wrong credentials';
    }
    


    The OLD standard library

    Please note: you should not be needing this anymore! This is only here for historical purposes.

    Take a look at: Portable PHP password hashing framework: phpass and make sure you use the CRYPT_BLOWFISH algorithm if at all possible.

    Example of code using phpass (v0.2):

    HashPassword( $password );
    
    // $hash would be the $hash (above) stored in your database for this user
    $checked = $pwdHasher->CheckPassword($password, $hash);
    if ($checked) {
        echo 'password correct';
    } else {
        echo 'wrong credentials';
    }
    

    PHPass has been implemented in some quite well known projects:

    • phpBB3
    • WordPress 2.5+ as well as bbPress
    • the Drupal 7 release, (module available for Drupal 5 & 6)
    • others

    The good thing is that you do not need to worry about the details, those details have been programmed by people with experience and reviewed by many folks on the internet.

    For more information on password storage schemes, read Jeff`s blog post: You're Probably Storing Passwords Incorrectly

    Whatever you do if you go for the 'I'll do it myself, thank you' approach, do not use MD5 or SHA1 anymore. They are nice hashing algorithm, but considered broken for security purposes.

    Currently, using crypt, with CRYPT_BLOWFISH is the best practice.
    CRYPT_BLOWFISH in PHP is an implementation of the Bcrypt hash. Bcrypt is based on the Blowfish block cipher, making use of it's expensive key setup to slow the algorithm down.

提交回复
热议问题