2009-10-19 2 views
0

안전한 사용자 인증 시스템을 구축하려고합니다.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>'; 
} 
} 
+0

무엇이'$ stat_salt'입니까? – Gumbo

+0

아, 데이터베이스 쿼리가 필요하지 않습니다. 'hash' 호출의 반환 값을'$ row [ 'password']'의 값과 비교하십시오. – Gumbo

+0

stat_salt는 모든 사용자에게 소금입니다. –

답변

5

제대로 dB로 삽입 않고 [...]

당신이 그것을 테스트 어떻게 ? md5은 32 자리 문자열을, hash('sha256', ...)은 64 자리 문자열을 반환합니다. password 필드를 수용 할 수있을만큼 길어야합니까? 삽입되지 않은 경우 $hash 삽입시 필드의 길이로 잘려서 선택에 대한 비교가 실패합니다.

+0

그렇지 않은 경우에도 잘린 SHA 결과가 같지 않아야합니까? – fbrereto

+0

클리핑 결과와 어떤 관련이 있습니까? OP의'varchar's를 비교했습니다. 내 암호 필드가 문제가되는 – SilentGhost

+1

입니다. varchar 감사합니다. muich! –

관련 문제