How to check a mysql encrypt value with a salt in PHP?

后端 未结 1 679
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-06 14:15

For my website I\'ve stored my user passwords in the database using this MySQL function:

ENCRYPT(\'password\', CONCAT(\'$6$\', SUBSTRING(SHA(RAND()), -16)))
         


        
相关标签:
1条回答
  • 2021-01-06 14:43

    The salt used by ENCRYPT() (better known as the crypt() function) is stored as part of the hash, and can be used as part of the hash:

    SELECT ... FROM users WHERE ... AND password = ENCRYPT('swordfish', password);
    

    (That is, if the password the user entered was "swordfish". I'm avoiding "password" because it's also a column name.)

    You can (and should) do the same thing in PHP by checking:

    crypt($user_password, $hashed_password) == $hashed_password
    

    Note that crypt() is not a particularly secure method of password storage. Please see Secure hash and salt for PHP passwords for details.

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