让我的网站更安全,我使用password_hash是否正确


Making my site more secure, Am I using password_hash correct?

我有一个非常小的网站,最近我一直在努力提高它的安全性,我曾经用纯文本存储我的密码。

我认为我做得对,但作为一名"业余"程序员,我想确保这一点,所以我问你,专业人士

当用户注册时,我会执行:password_hash($their_password, PASSWORD_DEFAULT)并将其存储在users表的"password"列中。我使用PASSWORD_DEFAULT,因为根据php.net,这似乎是最好的。

请注意,这个常数是为了随着时间的推移而变化,因为PHP中添加了新的更强的算法。"

听起来不错!

登录部分(非常简单):

if (count($_POST) > 0) {
$username = trim($_POST['username']);
$password = trim($_POST['password']);
$query = $db->prepare("SELECT password FROM users WHERE username = ?");
$query->execute(array($username));
$row = $query->fetch();
if (password_verify($password, $row['password'])) {
    echo "Correct password";
    // create session...
} else {
    // wrong password
}

也许我应该先检查一下用户名是否存在,但除此之外,你怎么看?

您似乎已经完全理解了文档以及如何构建所需的代码。为你暂时使用明文密码而感到羞耻,但你决定用正确的方法修复(即不是像me这样的md5一个愚蠢的人(我真的需要更新我的密码保存系统…))真是太棒了。

我能看到的唯一问题是,有些人的密码可能以空格开头或结尾。这样的密码将丢失其前导/尾随空格,事实上,用户可能会担心他们可以使用两个空格登录,或者不使用空格!因此,最好删除那些trim调用;)

你好:)我也是一个业余爱好者,尽管我不太了解魔法是如何发生的,但我认为我可以为你指明正确的方向。1) 用户输入他的密码,程序使用某种方法加密,加密后的密码就会被保存。2) 砰。没有人能看到原始密码是什么,甚至连输入密码的用户都看不到。登录时,该操作会重复,"用户输入的登录密码"会通过相同的过程进行加密,然后将其与同样加密的保存密码进行比较。如果它们是相同的未加密原始文件,则它们应该匹配。

为了额外的安全性,有时会在加密过程中"添加"一种名为"盐"的东西,使破解密码变得更加困难。假设有人不知怎么掌握了你的加密代码和加密密码列表,并试图通过对你的代码进行逆向工程来恢复这个过程?好吧,现在那个人有了额外的工作,找到你的"盐"是什么。。。(它可以是保存在服务器中的字符串,一个聪明的"玩月份日期"技巧,等等……很多选项)。这是我读到的东西。有很多建议可以让你开始。更多:

我用这个:几年前我从互联网上的某个地方得到的

 function encryptTheString($password, $salt, $iter_count=4096, $keylen=64,         $hash_alg= 'sha256' ) 
 {
     // Compute the length of hash alg output.
     // Some folks use a static variable and save the value of the hash len.
     // Considering we are doing 1000s hmacs, doing one more won't hurt.
     $hashlen = strlen(hash($hash_alg, null, true));
     // compute number of blocks need to make $keylen number of bytes
     $numblocks = ceil($keylen / $hashlen);
     // blocks are appended to this
     $output = '';
     for ($i = 1; $i <= $numblocks; ++$i) {
         $block = hash_hmac($hash_alg, $salt . pack('N', $i), $password, true);
         $ib = $block;
         for ($j = 1; $j < $iter_count; ++$j) {
             $block = hash_hmac($hash_alg, $block, $password, true);
             $ib ^= $block;
         }
         $output .= $ib;
     }
     // extract the right number of output bytes
     return substr($output, 0, $keylen);
 }

像这样的电话

$ePassword=ANDYETpbkdf2($password,"111111111122222222223333333333444444444455555555566666666661234");

完全可以:)给sha256读一读,开始进一步的启蒙。