Why is the PHP crypt() function returning the same thing for two different strings?

后端 未结 3 1947
栀梦
栀梦 2021-01-22 23:49

I\'m using PHP\'s crypt function for password hashing/encryption, but I don\'t think I am doing it right because \"nathan12\" and \"nathan123\" both allow me to log

相关标签:
3条回答
  • 2021-01-23 00:27

    You should use more than just a password salt to encrypt passwords.

    You can store a random string in your configuration file.

    $config['passwordKey'] = 'asjdfa783#H$Khjsdfhas78a734J%JSDGK2348235hxmfdA';
    

    And append it to $salt when encrypting. This way if the database is compromised, and your file system is not, then attackers can't decrypt your database password hashes. This should be essential to protect the users information on other sites with identical login information.


    To hash your passwords, password_hash is a simple crypt() wrapper specially configured for password hashing! (source)

    $password = password_hash($password, PASSWORD_BCRYPT, array(
        'cost' => 60,
        'salt' => $salt . $config['passwordKey']
    ));
    
    0 讨论(0)
  • 2021-01-23 00:34

    You should be using password_hash() instead of crypt, for the reasons you mention: "I'm probably not even using the crypt function properly". You say you are getting the salt from the DB... this sounds insecure. with password_hash() you can let PHP handle the salting for you in a secure manner.

    More details on why this is superior: http://www.sitepoint.com/hashing-passwords-php-5-5-password-hashing-api/

    0 讨论(0)
  • 2021-01-23 00:44

    The standard DES-based crypt() [...] only uses the first eight characters of str, so longer strings that start with the same eight characters will generate the same result (when the same salt is used).

    source

    Use a salt that starts with $<algo>$ to use something other than DES. See the crypt() documentation for details.

    0 讨论(0)
提交回复
热议问题