2014-09-25 1 views
1

mysql 데이터베이스에 사용자 이름과 암호화 된 비밀번호를 저장합니다. 테스트 용으로 암호를 데이터베이스에 암호화되지 않은 형식으로 저장합니다.PHP password_verify는 하나의 문자열로 작동하지만 다른 것으로 간주되지 않습니다.

다음 코드에서 해시 된 암호와 데이터베이스의 암호화되지 않은 암호를 얻습니다. 그런 다음 암호화되지 않은 암호를 암호화합니다.

주어진 암호가 저장된 해시 또는 새 해시에 대한 암호 확인 테스트를 통과하지 못합니다.

저장된 암호는 저장된 해시와 새 해시 모두에 대한 암호 확인 테스트를 통과합니다.

strcmp를 호출하면 저장된 암호와 주어진 암호가 같다고 표시됩니다.

어떻게 될 수 있습니까?

[편집] 웹 페이지의 사용자 입력에서 $ password를 전달 중입니다.

// get hashed password from database 
$sql = "SELECT member_password FROM member WHERE member_username=:username;"; 
$stmt = $db->prepare($sql); 
$stmt->bindParam("username", $username); 
$stmt->execute(); 
$hash = $stmt->fetch(PDO::FETCH_ASSOC); 
$hash = $hash["member_password"]; 

// get unencrypated password from database 
$sql = "SELECT member_unencrypted FROM member WHERE member_username=:username;"; 
$db = getConnection(); 
$stmt = $db->prepare($sql); 
$stmt->bindParam("username", $username); 
$stmt->execute(); 
$unencrypted = $stmt->fetch(PDO::FETCH_ASSOC); 
$unencrypted = $unencrypted["member_unencrypted"]; 

// encrypt the unencrypted password that was retrieved from the database 
$encrypted = password_hash($unencrypted, PASSWORD_DEFAULT); 

// given password does not pass the new hash per this test 
if(password_verify($password, $encrypted)) 
    echo '<br>given password passed new hash'; 
else 
    echo '<br>given password did not pass new hash'; 

// stored password does pass the new hash per this test 
if(password_verify($unencrypted, $encrypted)) 
    echo '<br>stored password passed new hash'; 
else 
    echo '<br>stored password did not pass new hash'; 

// given password does not pass the stored hash per this test. 
if(password_verify($password, $hash)){ 
    echo '<br>given password passed stored hash'; 
else 
    echo '<br>given password did not pass stored hash'; 

// stored password does pass the stored hash per this test. 
if(password_verify($unencrypted, $hash)) 
    echo '<br>stored password passed stored hash'; 
else 
    echo '<br>stored password did not pass stored hash'; 

// stored and given passwords are equal per this test. 
if(strcmp($unencrypted, $password)) 
    echo '<br>stored and given passwords are equal'; 
else 
    echo '<br>stored and given passwords are not equal'; 

출력 :

given password did not pass new hash 
stored password passed new hash 
given password did not pass stored hash 
stored password passed stored hash 
stored and given passwords are equal 
+1

해시를 저장하는 데 사용되는 열의 구조는 무엇입니까? – Mike

+1

또한 테스트 때문에 간단하게 수행하고 있는지 잘 모르겠지만 두 쿼리를 쉽게 결합 할 수 있습니다. 그리고 단일 값을 선택할 때'$ hash = $ stmt-> fetch (PDO :: FETCH_ASSOC)와 같은 것들 대신'fetchColumn()'을 사용해야합니다; $ hash = $ hash [ "member_password"]; ' – Mike

+0

팁 마이크에게 감사드립니다! SQL을 많이 작성한 이후로 꽤 오랜 시간이 걸렸습니다. 유용합니다. 해시는 VARCHAR (255)에 저장됩니다. – brucebolick

답변

2

그것은 변수 $password가에서 오는 질문에서 명확하지 않다, 그것은 데이터베이스에 저장된 것과 동일한 텍스트가 포함되어있는 경우.

이러한 테스트를 수행하는 경우 함수 password_hash()은 고유 한 소금을 생성하고 결과 해시에 포함한다는 점에 유의해야합니다. 확인을 위해 해시 값을 비교하려면이 소금 (및 기타 매개 변수)이 필요합니다. 즉, 해시 값이 매번 달라지며 password_hash()을 두 번 호출하면 결국 다른 소금과 비교할 수없는 해시가 생깁니다.

해시 형식을 다른 answer으로 설명하려고했습니다.

[브루스 편집 : 답변에 대한 의견보기]

+0

응답 해 주셔서 감사합니다! $ _GET에서 $ password가 나옵니다. 저장되어있는 암호화되지 않은 암호는 저장된 해시와 새로운 해시와 함께 작동한다는 것이 혼란 스럽습니다. 내가 전달한 암호는 각 해시에 대해 실패합니다. 그런 다음 저장된 암호와 내가 통과 한 암호를 비교할 때 암호가 같다고합니다. – brucebolick

+1

@brucebolick - HTML 페이지에 특수 인코딩이 있습니까, UTF-8입니까? 그리고 데이터베이스의 캐릭터 세트는 무엇입니까? 인코딩에 대해 잘 모르는 경우이 작은 [article] (http://www.martinstoeckli.ch/php/php.html#utf8)을 살펴보십시오. 선행/후행 공백도 검사하십시오. – martinstoeckli

+1

@brucebolick - 변수'$ password = 'yourpassword';를 설정하면 어떻게됩니까? – martinstoeckli

관련 문제