php – Blowfish加密 – 哈希已创建,但不会验证
我刚刚写了这段代码,我现在正在为一个新项目复兴,但它似乎没有用,我不能为我的生活弄清楚为什么它不会验证哈希.
当注册第一个passwordEncrypt()函数时,下面运行2个函数. 当尝试登录时,调用checkPassword()函数,而不是登录并回显“是”,它将进入回声“否”的部分. 所以,如果一双新鲜的眼睛可以提前看到许多感谢! // Encrypt user password function passwordEncrypt($password) { // set the salt $salt = substr(md5(time()),22); // encrypt using blowfish with a load of 10 $password = crypt($password,'$2a$10$' . $salt); // return the encrypted hash return $password; } /* Check password function when logging in first we select the password from the supplied username from the database // get the row and set the hash to the currect password from the database //run the salts etc and check to see if the passwords match */ function checkPassword($userName,$password,$db){ $sql = 'SELECT password FROM users WHERE userName = :userName'; $stmt = $db->prepare($sql); $stmt->bindValue(':userName',$userName,PDO::PARAM_STR); $stmt->execute(); $numRows = $stmt->rowCount(); if ($numRows > 0) { $row = $stmt->fetch(); $hash = $row['password']; // run the hash function on $password? $fullSalt = substr($hash,29); $new_hash = crypt($password,$fullSalt); // Check that the password matches if($hash == $new_hash) { echo 'yes'; exit; return true; } else { echo 'no'; exit; return false; } } else { echo 'way'; exit; return false; } } 我已经注册了密码,然后尝试了,这就是它返回的内容 密码:$2A $10 $023d3086e8462207a1fecueWH4Ub40MWbQJ7F9 因此它增加了hapWU3lYxlg3AAa 解决方法
和
你去吧色谱柱的长度太短,需要为60. 手册建议255. 您需要清除行,将列更改为60或更高,然后创建新哈希并再次登录.
是60长 脚注: 有人说有些人发现很难使用crypt(),并且使用password_hash()或兼容包(如果PHP <5.5)https://github.com/ircmaxell/password_compat/实际上更容易.这是你的选择. 请参阅此堆栈上的Q& A: > What is the output length of PHP crypt()? (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |