안전한 사용자 인증 시스템을 구축하려고합니다.md5에서 sha256으로 변경
코드는 http://net.tutsplus.com/tutorials/php/simple-techniques-to-lock-down-your-website/
에서이다 그러나 임 MD5에서 SHA-256로 변경하려고하지만 그것은 늘 로그인.
난 그냥 제대로 dB로 삽입 않습니다
$auth_pass = hash('sha256', $row['salt'] . $password . $stat_salt);
에
$auth_pass = md5($row['salt'] . $password . $stat_salt);
변경하지만 어떤 이유로 로그인 부분 작동 실 거예요. md5에서는 작동하지만 sha256에서는 작동하지 않습니다. 다른 방법으로 sha256을 사용해야합니까?
등록 :
// generate a unique salt
$salt = uniqid(mt_rand());
// combine them all together and hash them
$hash = hash('sha256', $salt . $password . $stat_salt);
// insert the values into the database
$register_query = mysql_query("INSERT INTO users (username, password, salt) VALUES ('".$username."', '".$hash."', '".$salt."')") or die("MySQL Error: ".mysql_error());
로그인
// grab the row associated with the username from the form
$grab_row = mysql_query("SELECT * FROM users WHERE username = '".$username."'") or die ("MySQL Error: ".mysql_error());
// if only one row was retrieved
if (mysql_num_rows($grab_row) == 1) {
// create an array from the row fields
$row = mysql_fetch_array($grab_row);
// re-hash the combined variables
$auth_pass = hash('sha256', $row['salt'] . $password . $stat_salt);
// check the database again for the row associated with the username and the rehashed password
$checklogin = mysql_query("SELECT * FROM users WHERE username = '".$username."' AND password = '".$auth_pass."'") or die("MySQL Error: ".mysql_error());
// if only one row is retrieved output success or failure to the user
if (mysql_num_rows($checklogin) == 1) {
echo "<h1>Yippie, we are authenticated!</h1>";
} else {
echo '<h1>Oh no, we are not authenticated!</h1>';
}
} else {
echo '<h1>Oh no, we are not in the database!</h1>';
}
}
무엇이'$ stat_salt'입니까? – Gumbo
아, 데이터베이스 쿼리가 필요하지 않습니다. 'hash' 호출의 반환 값을'$ row [ 'password']'의 값과 비교하십시오. – Gumbo
stat_salt는 모든 사용자에게 소금입니다. –