(PHP)如何在CRYPT_BLOWFISH中使用crypt()?

Ton*_*ark 4 php crypt

首先,我看到要使用CRYPT_BLOWFISH,我需要使用以$ 2a $开头的16个char盐.但是,crypt()php.net文档说某些系统不支持CRYPT_BLOWFISH.这种情况多久一次?

接下来,从他们在docs上的例子中,我看到我使用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!";
}
?>
Run Code Online (Sandbox Code Playgroud)

为了使用CRYPT_BLOWFISH,我唯一需要修改的是第一行来使它像这样;

crypt('mypassword', '$2a$07$usesomesillystringforsalt$')
Run Code Online (Sandbox Code Playgroud)

然后剩下的线路都很好吗?

sym*_*ean 5

对于5.3.0之前的PHP,crypt()使用了OS提供的lib.如果您使用的是早期版本,那么您需要检查您的操作系统文档以查看它是否受支持(检查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
      ....
Run Code Online (Sandbox Code Playgroud)

您只需在创建密码时指定前缀($ 2a $).

HTH

C.