Comparing passwords with crypt() in PHP

前端 未结 6 1078
谎友^
谎友^ 2020-12-02 21:01

I need to get the basics of this function. The php.net documentation states, for the blowfish algorithm, that:

Blowfish hashing with a salt as follow

6条回答
  •  难免孤独
    2020-12-02 21:46

    Following code example may answer your questions.

    To generate hashed password using Blowfish, you first need to generate a salt, which starts with $2a$ followed by iteration count and 22 characters of Base64 string.

    $salt = '$2a$07$usesomadasdsadsadsadasdasdasdsadesillystringfors';
    $digest = crypt('rasmuslerdorf', $salt);
    

    Store the whole $digest in database, it has both the salt and digest.

    When comparing password, just do this,

      if (crypt($user_input, $digest) == $digest)
    

    You are reusing the digest as salt. crypt knows how long is the salt from the algorithm identifier.

提交回复
热议问题