簡體   English   中英

如果我們使用鹽技術,如何對照輸入的密碼檢查Mysql數據庫中的密碼?

[英]How to check the password in Mysql Database against the entered one if we are using salt technique?

我的網站上有登錄和注冊系統,我想使用一種強大的方式來加密用戶密碼,然后再將其存儲在MySQL數據庫中。 我使用以下代碼對密碼進行加密:

function better_crypt($input, $rounds = 7)
  {
    $salt = "";
    $salt_chars = array_merge(range('A','Z'), range('a','z'), range(0,9));
    for($i=0; $i < 22; $i++) {
      $salt .= $salt_chars[array_rand($salt_chars)];
    }
    return crypt($input, sprintf('$2a$%02d$', $rounds) . $salt);
  }

  $user_password = "123456";
  $password_hash = better_crypt($user_password);
  //$password_hash = "$2a$07$t8Omz2TZhz5u0AI3l8uB4uQxzqYZCoqEsQmRo1gr.Viq5UnNReGMy";=> store in database 

當用戶嘗試登錄時,我使用它來檢查密碼:

$password_entered = "123456";

  $database_password_hash = "$2a$07$t8Omz2TZhz5u0AI3l8uB4uQxzqYZCoqEsQmRo1gr.Viq5UnNReGMy";// I get this from database depending on the username

  if(crypt($password_entered, $database_password_hash) == $database_password_hash) 
  {
    echo 'password is correct';
  }
  else
  {
    echo 'not correct';
  }

我使用crypt因為我的PHP版本不支持password_verify

我的問題是:我仍然not correct始終not correct

我想給每個用戶一個不同的salt' and I want to check it by隱窩salt' and I want to check it by

我必須更改此代碼中的任何內容嗎?為什么它not correct顯示?

您是否知道password_hash()函數有兼容包 可能您可以直接使用此功能,之后再切換到新的PHP版本時,只需將包含內容刪除到該庫中,代碼仍然可以使用。

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

// 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);

該函數起作用是因為它在結果的哈希值中包含了鹽。 函數password_verify()可以從那里提取它。 實際上,它是由crypt()函數完成的,因此您的代碼也將包含salt。

雙引號字符串中的美元符號作為變量插入,因此示例中$ database_password_hash的實際值為'$ 2a $ 07.Viq5UnNReGMy'。 請改用單引號。

這對我有用:

$password_entered = '123456';
$database_password_hash = '$2a$07$t8Omz2TZhz5u0AI3l8uB4uQxzqYZCoqEsQmRo1gr.Viq5UnNReGMy';// I get this from database depending on the username
if(crypt($password_entered, $database_password_hash) === $database_password_hash) {
  echo 'password is correct';
} else {
  echo 'not correct';
}

首先,在使用SALT創建哈希時,您需要同時返回哈希值和用於創建哈希值的鹽。

function better_crypt($input, $rounds = 7)
{
    $salt = '';
    $salt_chars = array_merge(range('A','Z'), range('a','z'), range(0,9));
    for($i=0; $i < 22; $i++) {
      $salt .= $salt_chars[array_rand($salt_chars)];
    }
    $salt = sprintf('$2a$%02d$', $rounds) . $salt);
    $hash = crypt($input, $salt);
    return( array('hash' => $hash, 'salt' => $salt);
}


$user_password = '123456';
$better_result = better_crypt($user_password);
// Store the $better_result['hash'] 
// AND 
// $better_result['salt'] 
// to the users table in the database

現在,當您驗證登錄時使用了正確的密碼時,您可以執行以下操作:

// Get from the database the users hashed password and the salt used to create the hash.

if( crypt($password_entered, $salt_from_users_row) == $database_password_hash) 
{
    echo 'password is correct';
} else {
    echo 'not correct';
}

地穴總是讓我感到困惑。 那就是為什么我只使用基本的哈希函數。

function verifyPassword($input, $dbHashOfPassword, $dbSavedSaltForThisUser)
{
    return $dbHashOfPassword === $this->hashPassword($input, $dbSavedSaltForThisUser);
}

function hashPassword($password, $salt)
{
    return hash('SHA512', $password.$salt);
}

function createSalt($username, $userid, $systemTime, $anything)
{
    //Do stuff to create a Unique Salt for your user.
    //This has to be stored in the database as well but 
    //will have the impact that every attacked userdata will 
    //need there own rainbow table created.
    return hash('MD5', 'T-'.$systemTime.'-U-'.$username.'-A-'.$anything.'-ID-'.$userid.'-E');
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM