为什么是crypt()产生不同的结果? [英] Why is crypt() generating different results?

查看:174
本文介绍了为什么是crypt()产生不同的结果?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Crypt正在使用相同的输入数据生成不同的哈希值,以下功能的哈希发生器/检查不再适用于验证用户:

 $ code public static function blowfish($ password,$ storedpass = false){
//如果加密数据被传递,请检查它是否符合输入($ info)
if($ storedpass) {
if(substr($ storedpass,0,60)== crypt($ password,$ 2y $ 08 $。substr($ storedpass,60))){
return true;
} else {
return false;
}
} else {
// make a salt and hash it with input,and salt to end
$ salt =; ($ i = 0; $ i <22; $ i ++){
$ salt。= substr(./ ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789,mt_rand(0,63),1);

}
// return 82 char string(60 char hash& 22 char salt)
return crypt($ password,$ 2y $ 08 $。$ salt)$ salt;
}
}

我把头撞在墙上,在Zend的内部算法与PHP与操作系统算法之间的差异中没有发现任何答案;或者PHP 5.3.8之前的版本或更早的版本...



编辑:我的问题在技术上得到回答,这是我的错,我没有正确的问。我实现了:

  $ salt = substr(bin2hex(openssl_random_pseudo_bytes(22)),0,22); 
// for($ i = 0; $ i <22; $ i ++){
// $ salt。= substr(./ ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789,mt_rand(0,63),1) ;
//}

我真正的问题是为什么以下函数返回不同?

  print(substr($ storedpass,0,60))< br /> ;); 

返回:$ 2y $ 08 $ 43f053b1538df81054d4cOJyrO5 / j7NtZBCw6LrFof29cLBs7giK6

  print(crypt($ password,$ 2a $ 08 $。substr($ storedpass,60))); 

返回:$ 2a $ 08 $ 43f053b1538df81054d4cOPSGh / LMc0PZx6RC6PlXOSc61BKq / F6。

解决方案

由于您在随机数字的帮助下创建 salt / p>

功能 mt_rand() 将在每次调用时创建随机数,可选择使用min,max参数。通常,对于强密码密码散列,应使用加密安全伪随机数生成器(CSPRNG)生成盐。



然后来问题,我认为会有ZEND和PHP之间的算法没有差别。因为zend是围绕核心php的框架,并且使用它。



要验证密码,如何 crypt 检查工作是

  crypt($ password,$ stored_hash)== $ stored_hash; 

一旦您首先哈希存储哈希值,这将很容易验证。 p>

这是在这里实际发生的情况,如果将hash作为第二个参数传递给函数blowfish,它将返回一个bool值的验证,而不考虑盐。 p $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ storagepass,60))){
return true;
} else {
return false;
}

有关散列和安全性的信息,请阅读这个



希望这有帮助


Crypt is generating different hashes with the same input data, and the [following] previously functional hash generator/check is no longer working for authenticating users:

public static function blowfish($password, $storedpass = false) {
    //if encrypted data is passed, check it against input ($info) 
      if ($storedpass) { 
            if (substr($storedpass, 0, 60) == crypt($password, "$2y$08$".substr($storedpass, 60))) { 
                return true; 
            }  else { 
                return false; 
            } 
      }  else { 
          //make a salt and hash it with input, and add salt to end 
          $salt = ""; 
          for ($i = 0; $i < 22; $i++) { 
            $salt .= substr("./ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789", mt_rand(0, 63), 1); 
          } 
          //return 82 char string (60 char hash & 22 char salt) 
          return crypt($password, "$2y$08$".$salt).$salt; 
     }
}

I'm banging my head against the wall and have found no answers in differences between Zend's internal algorithms vs PHP vs operating system algorithms; or variations between PHP 5.3.8 vs earlier...

EDIT: My question is technically answered, and it is my fault I didn't ask properly. I've implemented:

$salt = substr(bin2hex(openssl_random_pseudo_bytes(22)), 0, 22);
          //for ($i = 0; $i < 22; $i++) { 
            //$salt .= substr("./ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789", mt_rand(0, 63), 1); 
          //} 

My real question is; why are the following functions returning differently?

print(substr($storedpass, 0, 60)."<br />");

returns: $2y$08$43f053b1538df81054d4cOJyrO5/j7NtZBCw6LrFof29cLBs7giK6

print(crypt($password, "$2a$08$".substr($storedpass, 60)));

returns: $2a$08$43f053b1538df81054d4cOPSGh/LMc0PZx6RC6PlXOSc61BKq/F6.

解决方案

Because you are creating the salt with the help of random numbers,

The function mt_rand() will create random number each time when you are calling, optionally with min, max parameters. Usually for strong cryptography password hashing, Salt should be generated using a Cryptographically Secure Pseudo-Random Number Generator (CSPRNG).

Then come to your problem, i assume there will be no difference in algorithm between ZEND and php. Because zend is a framework wrapping around the core php, and make use of it.

To verify the password, how the crypt check work is

crypt($password, $stored_hash) == $stored_hash;

Once you stored the hash when you hash first, it will be easy to verify by this.

That is what actually happens here, if you pass the hash as second parameter to the function blowfish, it will return the verification by a bool value, regardless of the salt.

if (substr($storedpass, 0, 60) == crypt($password, "$2y$08$".substr($storedpass, 60))) { 
    return true; 
}  else { 
    return false; 
}

for your information about hashing and security read this

Hope this helps

这篇关于为什么是crypt()产生不同的结果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆