Hash md5到sha1密码哈希更改
基本上,我有一个游戏服务器,其中的密码哈希为sha1,另一个为md5,用于新用户注册帐户时使用。我决定坚持使用sha1,但是我希望我的用户能够更改他们的密码,从而将他们从md5创建到sha1中 我已经编写了下面的代码,但它似乎不起作用 基本上,我希望当他们更改密码时,它会将md5替换为sha1哈希密码Hash md5到sha1密码哈希更改,hash,md5,sha1,Hash,Md5,Sha1,基本上,我有一个游戏服务器,其中的密码哈希为sha1,另一个为md5,用于新用户注册帐户时使用。我决定坚持使用sha1,但是我希望我的用户能够更改他们的密码,从而将他们从md5创建到sha1中 我已经编写了下面的代码,但它似乎不起作用 基本上,我希望当他们更改密码时,它会将md5替换为sha1哈希密码 <?php if(isSet($_POST['submit'])) { $changePW = true; } ?> <?php public function has
<?php
if(isSet($_POST['submit']))
{
$changePW = true;
}
?>
<?php
public function hashed($password)
{
return sha1($password . "xCg532%@%gdvf^5DGaa6&*rFTfg^FD4\$OIFThrR_gh(ugf*/");
}
?>
<?php
if(isset($changePW))
{
//host, username, password, dbName
$con=mysqli_connect("localhost","root","password","dbName");
$username = $_POST['username'];
$password = $_POST['passwordOld'];
$passwordNew1 = $_POST['passwordNew1'];
$passwordNew2 = $_POST['passwordNew2'];
$passwordHash = md5($password);
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$cmd = "SELECT COUNT(*) FROM users WHERE username = '" . $username . "' AND password = '" . $passwordHash . "'";
$result = mysqli_query($con,$cmd);
$row = mysqli_fetch_row($result);
if($row[0] == 1)
{
if($passwordNew1 == $passwordNew2)
{
$newHash = hashed($passwordNew1);
$cmd = "UPDATE users SET password = '$newHash' WHERE username = '$username'";
mysqli_query($con,$cmd);
}
else{
$passwordMatch = true;
}
}
else {
$detailsError = true;
}
//$hash = md5($password);
//mysqli_query($con,"INSERT INTO tutorials_tbl (name, hash) VALUES ('" . $username . "','" . $hash . "')");
mysqli_close($con);
}
?>
<head>
<style type="text/css">
.lblLabel{
width:165px;
display: inline-block;
}
</style>
</head>
<form name="login" method="post" action="change.php">
<fieldset style="width:350px;"><legend>Change Password Form</legend>
<label class="lblLabel">Username</label><input id="name" type="text" name="username" value=""><br />
<label class="lblLabel">Old Password</label><input type="password" name="passwordOld"><br />
<?php
if(isset($detailsError))
{ ?>
<span style="color: #F00; font-weight: bold;">Username or Password Incorrect</span>
<?php }
?>
<label class="lblLabel">New Password</label><input type="password" name="passwordNew1"><br />
<label class="lblLabel">Repeated New Password</label><input type="password" name="passwordNew2"><br />
<?php
if(isset($passwordMatch))
{ ?>
<span style="color: #F00; font-weight: bold;">Password's do not match</span>
<?php }
?>
<label class="lblLabel"> </label><input id="submit" type="submit" name="submit" value="Change Password"><br />
</fieldset>
</form>
正如我在评论中所解释的,要使用户在密码存储中的转换尽可能平滑,您需要做的是将每个人的密码与SHA1进行散列,而将其保留为MD5散列。然后,在您的正常登录逻辑中,您将MD5散列密码,然后SHA1散列密码
另一方面,我将使用SHA2作为散列算法(同样快速、更安全等)
每个密码存储系统都必须有切换到更好的哈希算法的选项,您的问题不是一次性迁移问题。像BCrypt这样的好密码散列算法有一个成本因素,有时您必须增加这个成本因素(因为更快的硬件),然后您需要与迁移完全相同的过程
这就导致了您最大的问题,SHA-1不适合散列密码,尤其是如果它是用固定盐腌制的。它速度太快,很容易使用暴力(2013年使用通用硬件)。这就是为什么我们应该使用像BCrypt这样的慢键派生函数。如果您努力迁移哈希,那么最好直接切换到BCrypt
切换到更好的散列算法的通常方法是等待用户下次登录(与您的示例相反,用户不需要更改密码)。然后采取以下步骤:
首先尝试用新算法验证输入的密码。新密码和已转换的密码将不会花费更长的时间进行验证
如果不匹配,请将其与旧的哈希算法进行比较
如果旧的散列值匹配,那么您可以计算并存储新的散列,因为您知道密码
该系统可以扩展到多个迁移。只需确保首先检查最新的算法,然后检查较旧的算法。然后,只有在用户下次登录时,登录时间才会更长,并且新用户不会受到向后兼容性问题的影响
这就留下了如何使用BCrypt的问题。PHP5.5将有自己的函数和密码_verify()就绪。我建议您使用这个优秀的api,或者它适用于早期的PHP版本。用法非常简单:
// Hash a new password for storing in the database.
// The function automatically generates a cryptographically safe salt.
$hashToStoreInDb = password_hash($password, PASSWORD_BCRYPT);
// Check if the hash of the entered login password, matches the stored hash.
// The salt and the cost factor will be extracted from $existingHashFromDb.
$isPasswordCorrect = password_verify($password, $existingHashFromDb);
➽ 请记住,您需要一个长度为60个字符的数据库字段来存储此类散列值。出于好奇,为什么不使用MD5的密码?另外,您看到了吗?我不熟悉密码散列。我有1000多个注册用户,所有用户都有MD5密码,需要切换到SHA1才能成功登录。基本上,你要做的是,只需对每个用户进行一次检查,然后SHA对他们的密码进行哈希运算。这将创建密码的SHA哈希。当您进行登录检查时,您需要对给定的密码进行MD5散列,然后对结果进行散列,然后对照您的用户表进行检查。这对您来说是最简单的初始工作,对您的用户来说是一个无缝的过渡(他们不必更改密码,您不必维护他们密码的两个记录,等等)。您的密码字段在数据库中有多大?它可能太小,这将导致密码截断,从而导致针对数据库进行验证的所有尝试都失败。
// Hash a new password for storing in the database.
// The function automatically generates a cryptographically safe salt.
$hashToStoreInDb = password_hash($password, PASSWORD_BCRYPT);
// Check if the hash of the entered login password, matches the stored hash.
// The salt and the cost factor will be extracted from $existingHashFromDb.
$isPasswordCorrect = password_verify($password, $existingHashFromDb);