doucheng7534 2012-01-28 12:38
浏览 309
已采纳

PHP在将密码存储到数据库之前加密密码

I found the following example on php.net to secure password before storing it. However, I don't quite understand line 3. Could someone explain this a little bit? Thanks!

1 <?php
2 $password = crypt('mypassword'); // let the salt be automatically generated

3 if (crypt($user_input, $password) == $password) {
4   echo "Password verified!";
5 }
6 ?>
  • 写回答

4条回答 默认 最新

  • duanlei4759 2012-01-28 12:47
    关注

    crypt is a one-way function and returns a string that already contains the salt,

    When comparing the user input with the crypt result, the function automatically extracts the salt from the string.

    To be more clear :

    crypt() outputs a string that contains both the salt and the result of the hash. When you pass it that string as a salt, it knows to extract only the salt part and ignore the hash part. And it still returns a string containing both the salt and the hash. So these strings can be compared

    You can clearly understand by:

    when user signups for the first time , the process is:

     $password = crypt($user_input); // let the salt be automatically generated
    
     if (crypt($user_input, $password) == $password) {
       echo "Password verified!";
     }
    

    when user tries to login , the process will be :

    if(crypt($user_passsword_currentlyin_db, $user_inputted_password) == $user_inputted_password) {
          echo "Password verified!";
    }
    

    Hope you get the thing :)

    EDIT:

    The output of crypt consists of:

    When you pass this output as "salt" back to crypt, it will extract the right algorithm and salt, and use these for the operation. If there is only an algorithm mentioned, it uses this one and generate random salt. Otherwise it will choose a default algorithm and generate random salt. The hash part in the passed salt parameter is ignored.

    So you can simply compare your stored_hash with crypt(password, stored_hash) - if it is equal, it quite likely was the right password.

    Here is an pseudocode explanation (in PHP-like syntax) how crypt works:

    function crypt($password, $salt)
    {
       if (substr($salt,0 1) == "_") {
         $count = substr($salt, 1, 4);
         $real_salt = substr($salt, 5, 4);
         return "_" . $count . $real_salt . crypt_ext_des($password, $count, $salt);
       }
       if(substr($salt, 0, 3) == "$1$") {
         list($ignored, $real_salt, $ignored) = explode("$", $salt);
         return "$1$" . $real_salt . "$" . crypt_md5($password, $real_salt);
       }
       if(substr($salt, 0, 4) == "$2a$") {
          $cost = substr($salt, 4, 2);
          $real_salt = substr($salt, 7, 22);
           return "$2a$" . $cost . "$" . $real_salt . crypt_brypt($password, $real_salt, $cost);
      }
      // ... SHA256 and SHA512 analogons
    
      // no match => STD_DES
      $real_salt = substr($salt, 0, 2);
      return $real_salt . crypt_std_des($password, $real_salt);
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)