Crypt() salt generation and password encryption, well executed?

后端 未结 1 1811
一个人的身影
一个人的身影 2020-12-19 13:47

these are some functions I am using for password encryption and password verification. Was wondering if this is a good way to handle it. I am using the codeigniter framework

相关标签:
1条回答
  • 2020-12-19 14:39

    There are some points that can be improved, but first i would recommend to use PHP's new function password_hash(). This function will generate a safe salt and includes it in the resulting hash-value, so you can store it in a single database field. There exists also a compatibility pack for earlier versions.

    // Hash a new password for storing in the database.
    // The function automatically generates a cryptographically safe salt.
    $hashToStoreInDb = password_hash($password, PASSWORD_BCRYPT);
    
    // Check if the hash of the entered login password, matches the stored hash.
    // The salt and the cost factor will be extracted from $existingHashFromDb.
    $isPasswordCorrect = password_verify($password, $existingHashFromDb);
    

    Some thoughts about your code:

    1. You generate a BCrypt hash with crypt(), so the salt will be part of the resulting hash. There is no need to store it separately.
    2. The generation of the salt can be improved, use the random source of the operating system MCRYPT_DEV_URANDOM.
    3. If you would change the cost factor to 9, the format would become invalid, because crypt expects two digits.
    0 讨论(0)
提交回复
热议问题