【问题标题】:(PHP) How to use crypt() with CRYPT_BLOWFISH?(PHP) 如何将 crypt() 与 CRYPT_BLOWFISH 一起使用?
【发布时间】:2010-02-10 10:04:46
【问题描述】:

首先,我看到要使用 CRYPT_BLOWFISH,我需要使用从 $2a$ 开始的 16 字符盐。但是,php.net documentation for crypt() 表示某些系统不支持 CRYPT_BLOWFISH。这种情况多久发生一次?

接下来,从他们在文档上的示例中,我看到我使用 crypt() 如下:

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

/* You should pass the entire results of crypt() as the salt for comparing a
   password, to avoid problems when different hashing algorithms are used. (As
   it says above, standard DES-based password hashing uses a 2-character salt,
   but MD5-based hashing uses 12.) */
if (crypt($user_input, $password) == $password) {
   echo "Password verified!";
}
?>

为了使用 CRYPT_BLOWFISH,我只需要修改第一行就可以了;

crypt('mypassword', '$2a$07$usesomesillystringforsalt$')

然后其余的行就可以了吗?

【问题讨论】:

    标签: php crypt


    【解决方案1】:

    对于 5.3.0 之前的 PHP,crypt() 使用操作系统提供的库。如果您使用的是早期版本,那么您需要检查您的操作系统文档以查看它是否受支持(检查 CRYPT_BLOWFISH 常量的值) - 如果不支持,则该算法在 PHP 的 mcrypt() 扩展中实现。

    您从文档中引用的示例似乎没有多大意义:

      $stored_password=fetch_password($user);
    
      if (crypt($_REQUEST['password'],$stored_password)===$stored_password) {
          // note that crypt automatically extracts the salt and alogrithm type
          // from $stored_password
          ....
    

    创建密码时只需要指定前缀($2a$)即可。

    HTH

    C.

    【讨论】:

    • 是的,但我剩下的唯一问题是:当我创建密码时,我使用 crypt('mypassword', '$2a$07$usesomesillystringforsalt$'),对吗?我的盐实际上是一个随机生成的 16 个字符的字符串?
    猜你喜欢
    • 2020-05-10
    • 1970-01-01
    • 2016-10-29
    • 2013-06-20
    • 1970-01-01
    • 2013-05-05
    • 2014-01-06
    • 1970-01-01
    • 2013-05-11
    相关资源
    最近更新 更多