首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >(PHP)如何在CRYPT_BLOWFISH中使用crypt()?

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

Stack Overflow用户
提问于 2010-02-10 18:04:46
回答 1查看 11.4K关注 0票数 4

首先,我看到要使用CRYPT_BLOWFISH,我需要使用以$2a$开头的16个字符的盐。然而,php.net documentation for crypt()说有些系统不支持CRYPT_BLOWFISH。这种情况有多频繁?

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

代码语言:javascript
复制
<?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,我唯一需要修改的就是第一行,让它变成这样;

代码语言:javascript
复制
crypt('mypassword', '$2a$07$usesomesillystringforsalt$')

然后剩下的行就没问题了吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2010-02-10 18:24:56

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

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

代码语言:javascript
复制
  $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.

票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/2235897

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档