mae*_*416 23 php mysql password-storage
可能是一个非常新手的问题,但是,我一直在阅读并发现在理解密码的创建和存储方面存在一些困难.从我所读到的md5/hash密码是将它们存储在数据库中的最佳方式.但是,我该如何首先创建这些密码?
所以说我有一个用户bob和密码bob123的登录页面 - 我将如何1.将bobs密码输入数据库开始(哈希)2.如何检索并确认哈希密码?
谢谢
Joh*_*ehm 43
编辑2017/11/09:一定要看看O Jones的回答.
首先关闭MD5并不是你可以用于此尝试sha256或sha512的最大哈希方法
也就是说,让我们使用hash('sha256')而不是md5()代表流程的散列部分.
当您第一次创建用户名和密码时,您将使用一些盐来哈希原始密码(为每个密码添加一些随机的额外字符以使其更长/更强).
可能会从创建用户表单中看到这样的内容:
$escapedName = mysql_real_escape_string($_POST['name']); # use whatever escaping function your db requires this is very important.
$escapedPW = mysql_real_escape_string($_POST['password']);
# generate a random salt to use for this account
$salt = bin2hex(mcrypt_create_iv(32, MCRYPT_DEV_URANDOM));
$saltedPW = $escapedPW . $salt;
$hashedPW = hash('sha256', $saltedPW);
$query = "insert into user (name, password, salt) values ('$escapedName', '$hashedPW', '$salt'); ";
Run Code Online (Sandbox Code Playgroud)
然后在登录时它看起来像这样:
$escapedName = mysql_real_escape_string($_POST['name']);
$escapedPW = mysql_real_escape_string($_POST['password']);
$saltQuery = "select salt from user where name = '$escapedName';";
$result = mysql_query($saltQuery);
# you'll want some error handling in production code :)
# see http://php.net/manual/en/function.mysql-query.php Example #2 for the general error handling template
$row = mysql_fetch_assoc($result);
$salt = $row['salt'];
$saltedPW = $escapedPW . $salt;
$hashedPW = hash('sha256', $saltedPW);
$query = "select * from user where name = '$escapedName' and password = '$hashedPW'; ";
# if nonzero query return then successful login
Run Code Online (Sandbox Code Playgroud)
你必须在密码密码方面推理:
存储密码,就像md5('bob123');bob注册到您的应用程序一样
$query = "INSERT INTO users (username,password) VALUES('bob','".md5('bob123')."');
Run Code Online (Sandbox Code Playgroud)
然后,当bob登录时:
$query = "SELECT * FROM users WHERE username = 'bob' AND password = '".md5('bob123')."';
Run Code Online (Sandbox Code Playgroud)
obvioulsy使用用户名和密码的变量,这些查询是由php生成的,然后你可以在mysql上执行它们
请不要使用MD5进行密码散列.这些密码可以在几毫秒内破解.你肯定会受到网络犯罪分子的抨击.
PHP提供了一个高质量且面向未来的密码散列子系统,它基于可靠的随机盐和多轮Rijndael/AES加密.
当用户首次提供密码时,您可以像这样对其进行散列:
$pass = 'whatever the user typed in';
$hashed_password = password_hash( "secret pass phrase", PASSWORD_DEFAULT );
Run Code Online (Sandbox Code Playgroud)
然后,存储在MySQL的$hashed_password一varchar(255)列中.稍后,当用户想要登录时,您可以从MySQL检索散列密码并将其与用户提供的用于登录的密码进行比较.
$pass = 'whatever the user typed in';
$hashed_password = 'what you retrieved from MySQL for this user';
if ( password_verify ( $pass , $hashed_password )) {
/* future proof the password */
if ( password_needs_rehash($hashed_password , PASSWORD_DEFAULT)) {
/* recreate the hash */
$rehashed_password = password_hash($pass, PASSWORD_DEFAULT );
/* store the rehashed password in MySQL */
}
/* password verified, let the user in */
}
else {
/* password not verified, tell the intruder to get lost */
}
Run Code Online (Sandbox Code Playgroud)
这种面向未来的工作如何运作?PHP的未来版本将适应更快,更容易破解加密的匹配.如果有必要重新密码以使密码更难破解,那么password_needs_rehash()函数的未来实现 将检测到这一点.
不要重新发明爆胎.使用专业设计和审查的开源代码来确保安全.
插入:
INSERT INTO ... VALUES ('bob', MD5('bobspassword'));
Run Code Online (Sandbox Code Playgroud)
恢复:
SELECT ... FROM ... WHERE ... AND password=md5('hopefullybobspassword');
Run Code Online (Sandbox Code Playgroud)
你怎么直接在查询中做到这一点.但是,如果您的MySQL启用了查询日志记录,则密码的明文将写入此日志.所以...你想在你的脚本中进行MD5转换,然后将结果哈希插入到查询中.