2012-04-20 3 views
1

나는 두 가지 기능, HashPassword()ValidatePassword을 가지고 있습니다.PHP 암호 해시 비교가 작동하지 않는 이유는 무엇입니까?

첫 번째 버전은 등록 양식에 제공된 비밀번호를 동적 소금으로 해시하고 두 번째 버전은 비밀번호 유효성을 검사합니다.

기본적으로 일치하지 않는 암호가 일치하는지 확인하고 있습니다. 내 로그인 양식에서 ValidatePassword() 함수를 호출 할 때 ValidatePassword()의 해시를 반향 출력하여 비교할 수있는 쓰기 위치에서 해시를 분리하고 있지만 비교할 때 해시를 비교하면 다른 해시가 출력됩니다.

두 기능을 더 잘 설명하기 쉽습니다.

<?php 

// hashes a users password along with a dynamic salt 
// dynamic salt is stored with users password and is seperated by a ; 
function HashPassword($password){ 
    // creates a dynamic salt 
    $DynamicSalt  = uniqid('', true); 
    // hash the password given from user along with dynamic salt 
    $HashedPassword = hash('sha512', $password . $DynamicSalt); 
    // combine the hashed password seperated by ; then the dynamic salt to store in database 
    $final    = $HashedPassword.';'.$DynamicSalt; // this value is stored in database like: c29fc9e4acdd2962c4db3f108bee728cf015c8f6388ab2cd4f21e405f9d2f13b2d53a1ab8629aa21c3453906a98aff0d4b9a0e14bfc2c553a4f9c7c0c32fc58a;4f91cfc746b426.53641182 
    return $final; 
} 

// validate password user entered ($password = password from user | $dbHashedPassword = hash from database) 
function ValidatePassword($password, $dbHashedPassword){ 
    // we need to get the password hash before the salt, (fetch just the first 128 characters from database hash) 
    $CorrectHash = substr($dbHashedPassword, 0, 128); 
    // get the dynamic salt from end of sha512 hash (
    $DynamicSalt = substr($dbHashedPassword, 129, 151); // get just the dynamic salt part of the db hash 
    // hash the password from user and the dynamic salt which we got from the end of the hash from database 
    $TestHash = hash('sha512', $password . $DynamicSalt); 

    return ($CorrectHash == $TestHash); 


    // WHEN I ECHO OUT THE THREE VARIABLES $CorrectHash, $DynamicSalt and $TestHash 
    // THE $CorrectHash (from db, first 128 chars) is not the same as $TestHash 
    // AND TO MAKE SURE I AM SPLITTING THE HASH AND DYNAMIC SALT InN THE CORRECT PLACE I ECHO OUT 
    // $DynamicSalt and it is split in the correct place showing the 23 characters which the dynamic salt is 23 characters 
    // BUT WHEN I COMBINE THE $password and $DynamicSalt in $TestHash it shows a completely different hash from the $CorrectHash (which we got and split from database) 
} 

?> 

나는 무엇이 잘못되었는지 모르겠지만, 내가 밖으로 에코 때 다음 처음 128 개 문자 (SHA512) 동적 소금 (보여주기 때문에 나는 올바른 장소에 해시 및 동적 소금을 분할하고있어 보인다 23 문자)하지만 두 128 문자의 해시를 반향시킬 때는 일치하지 않습니다 (이 말은 완전히 다른 해시라는 의미입니다).

답변

1

아마도 테스트 할 해시를 어떻게 분할하는지와 관련이 있습니다. 예를 들어, 길이가 151자인 소금을 얻으려고합니다.

이 시도 :

function ValidatePassword($password, $dbHashedPassword) { 
    list($CorrectHash, $DynamicSalt) = explode(";",$dbHashedPassword,2); 
    return $CorrectHash == hash("sha512",$password.$DynamicSalt); 
} 
+0

안녕하세요,하지만 당신이 시도합니다,하지만 내가 동적 소금에서 올바르게 분할 된 128 char 해시가 세 변수를 반향 할 때, 그리고 때 내가 동적 소금 밖으로 (또한 acocunting, 그것은 그것을; 동적 인 소금 23 문자의 정확한 길이를 에코. 난 phpmyadmin에서 이것을 비교하고 올바른 위치에서 분할,하지만 그들은 완전히 다른 해시를 비교할 때. 감사합니다 phplover – PHPLOVER

+0

안녕하세요, 대단히 감사합니다! 당신과 그것을 테스트, 확실하지 않은 이유는 내 것이 작동하지 않는다는 것을 보여줍니다. 테스트 할 때 올바른 위치에 울리고 있지만 정확하게 비교하지는 못한다는 것을 보여줍니다. 불가능하다고 들리지만 내 3 변수를 반향시킬 때 해시와 라이트 영역에서의 동적 소금을 에코합니다. 감사합니다 phplover – PHPLOVER

2

을 모든 substr 방금 ​​BTW $TestHash

";" . $DynamicSalt을 추가하는 것을 잊었다 올바른지. 이것은 첫 번째 데이터베이스 정규화 규칙 인 "값은 원자 적이어야"에 위배됩니다. 소금은 별도의 필드에 보관해야합니다.

+0

안녕하세요, 나는 ';'에 대한 설명과 내가 의식 locatio에서 분할했다 보장하기 위해 n validatehash 함수와 데이터베이스의 해시 및 동적 소금에있는 세 변수를 반향시킴으로써 phpmyadmin에서 올바른 위치로 분할되어 있는지 확인하고이를 올바른 위치로 분할합니다. 또한 내가 보았던 충고는 소금을 데이터베이스에 저장해야한다는 것을 의미합니다. 감사합니다. – PHPLOVER

관련 문제