2016-12-27 13 views
0

私のテスト機能:塩がデータベースに正しく保存されていません。

<?php 

include_once("core/init.php"); 

$admin = new Admin(); 
$name = "akhil"; 
$password = "daydreamers"; 
$salt = Hash::salt(24); 
$hash = Hash::make($password,$salt); 
/*echo $hash; 
echo "<br/>";*/ 
$admin->newAdmin($name,$hash,$salt); 
$dsalt = $admin->getSalt($name); 
if($salt != $dsalt){ 
    echo "Wrong"; 
} 
/*echo Hash::make($password,$dsalt); 
echo "<br/>"; 
//$admin->verify($name,$password); 
echo $admin->getPassword($name);*/ 

?> 

ハッシュクラス:

<?php 

class Hash{ 
    public static function make($string,$salt=''){ 
     return hash('sha256', $string . $salt); 
    } 
    public static function salt($length){ 
     return mcrypt_create_iv($length, MCRYPT_DEV_URANDOM); 
    } 
} 

?> 

database structure

私が保管しています塩およびデータベースから取得塩が一致していません。私は列のサイズを増やすことを示唆しているように思われる他のポストを通過しましたが、機能しません。

答えて

1

パスワードで作業しているので、password_hash()機能に切り替えて、上記の安全でない実装について忘れてください。 password_hash()関数はsaltを気にしますので、別々に格納する必要はありません。ハッシュを単一のvarchar(255)フィールドに格納するだけです。

// Hash a new password for storing in the database. 
// The function automatically generates a cryptographically safe salt. 
$hashToStoreInDb = password_hash($password, PASSWORD_DEFAULT); 

// Check if the hash of the entered login password, matches the stored hash. 
// The salt and the cost factor will be extracted from $existingHashFromDb. 
$isPasswordCorrect = password_verify($password, $existingHashFromDb); 
関連する問題