2014-09-25 62 views
1

我使用PHPASS來存儲密碼加密和登錄時進行比較。使用PHPass來哈希密碼的麻煩

這裏是代碼

ob_start(); 
$userName = $password = ""; 
$userNameErr = $passwordErr = $loginErr = ""; 
$hasher = new PasswordHash(8, false); 

if (isset($_POST['subEmployee'])) { 
    if (empty($_POST['user_name'])) { 
     $userNameErr = "User name is required"; 

    } else { 
     $userName = check_input($_POST['user_name']); 
     if (!preg_match("/^[0-9_a-zA-Z]*$/", $userName)) { 
      $userNameErr = "Only letters, numbers and '_' allowed"; 
     } 
    } 
    if (empty($_POST['password'])) { 
     $passwordErr = "Password is required"; 
    }else{ 
     $password = check_input($_POST['password']); 
    } 

    $active = 1; 
    $loginUser = $db->prepare("SELECT password FROM users WHERE user_name=? AND activity=?"); 
    $loginUser->bind_param('si', $userName, $active); 
    if ($loginUser->execute()) { 
     $results = $loginUser->get_result(); 
     if ($results->num_rows == 1) { 
      $row = $results->fetch_object(); 
      $stored_hash = "*"; 
      $stored_hash = $row->password; 
      $check = $hasher->CheckPassword($password, $stored_hash); 
      if ($check) { 
       $_SESSION['name'] = $row->first_name; 
       $_SESSION['userId'] = $row->id; 
       $_SESSION['user'] = 1; 
       print_r($_SESSION); 
       header("Location:?pid=4"); 
      } elseif (!empty($_POST['user_name']) && !empty($_POST['password'])) { 
       $loginErr = "'Invalid Login Information'"; 
      } 
     } 
    } 
} 

到目前爲止,它總是給出相同的消息「無效的登錄信息」我已經存儲了密碼這樣的登記表。

$hasher = new PasswordHash(8, false); 
$hash = md5(rand(0, 1000)); 

if (empty($_POST['password'])) { 
     $error ['passwordErr'] = "Password is required"; 
    } elseif (strlen($_POST['password']) < 8) { 
     $error ['passwordErr'] = "<span class='notAllowed'>Chose password with at last eight characters</span>"; 
    } elseif (strlen($_POST['password']) > 72) { 
     $error ['passwordErr'] = "<span class='notAllowed'>Password max 72 characters</span>"; 
    } elseif ($_POST['password'] !== $_POST['confirm']) { 
     $error ['passwordErr'] = "Password don't matching"; 
    } else { 
     $password = $hasher->HashPassword($password); 
    } 

當我打開數據庫的密碼散列似乎對我的用戶名是存在的,一切正常

,但仍收到此消息爲「無效的登錄信息」。

做這兩條線是正確

$loginUser = $db->prepare("SELECT password FROM users WHERE user_name=? AND activity=?"); 
    $loginUser->bind_param('si', $userName, $active); 

確實登入碼確定。

我試試這個太

更新 我更新了我的代碼

if (isset($_POST['subEmployee'])) { 
    $error=array(); 

    $hash_cost_log2 = 8; 
    $hash_portable = FALSE; 
    $hasher = new PasswordHash($hash_cost_log2, $hash_portable); 

    if (empty($_POST['user_name'])) { 
     $userNameErr = "User name is required"; 

    } else { 
     $userName = check_input($_POST['user_name']); 
     if (!preg_match("/^[0-9_a-zA-Z]*$/", $userName)) { 
      $userNameErr = "Only letters, numbers and '_' allowed"; 
     } 
    } 
    if (empty($_POST['password'])) { 
     $passwordErr = "Password is required"; 
    } else { 
     $password = $_POST['password']; 
    } 
    $active = 1; 

    $loginUser = $db->prepare("SELECT password FROM hired_person_info WHERE user_name=? AND activity=?"); 
    $loginUser->bind_param('si', $userName, $active); 
    if ($loginUser->execute()) { 
     $results = $loginUser->get_result(); 
     if ($results->num_rows == 1) { 
      $row = $results->fetch_object(); 
      $stored_hash = "*"; 
      $stored_hash = $row->password; 
      $check = $hasher->CheckPassword($password, $stored_hash); 
      if ($check) { 
       $_SESSION['name'] = $row->first_name; 
       $_SESSION['userId'] = $row->id; 
       $_SESSION['user'] = 1; 
       print_r($_SESSION); 
       header("Location:?pid=4"); 
      } elseif (!empty($_POST['user_name']) && !empty($_POST['password'])) { 
       $loginErr = "'Invalid Login Information'"; 
      } 
     } else { 
      $loginErr = "'We didn't find any users'"; 
     } 
    } 
} 

從PHPass的手動添加此

$hash_cost_log2 = 8; 
     $hash_portable = FALSE; 
     $hasher = new PasswordHash($hash_cost_log2, $hash_portable); 

仍然沒有運氣有人可以告訴我在哪裏上午在這裏誤認爲

編輯 這是我check_input()代碼

function check_input($data) { 
    $data = trim($data); 
    $data = stripslashes($data); 
    $data = htmlspecialchars($data); 
    return $data; 
} 

而且我在使用PHP 29年3月5日

感謝

+0

請不要使用PHass,請使用原生PHP 5.5密碼哈希函數(也可用於PHP 5.3/5.4)。 – Sliq 2014-09-27 14:20:59

+0

您可以通過我的代碼指導我使用PHP 5.3的源代碼以及如何實現它到我的登錄代碼 – 2014-09-27 17:18:31

+0

,爲什麼不使用PHPass不好,有錯誤,非常困難或什麼? – 2014-09-27 17:20:01

回答

0

這些是一些點,我會檢查:

1)在你的你直接檢查POST變量的註冊處理程序,但爲了哈希,你需要一個變量$ password,我總是以同樣的方式訪問輸入,例如:

$password = $hasher->HashPassword($_POST['password']); 

2)函數check_input()不推薦用於密碼,因爲您計算散列值並且該散列值無論如何都是「安全」的。即使對於其他用戶輸入,也應該像您那樣對其進行驗證,但是應儘可能晚地進行轉義,並且僅針對特定輸出。所以不應該爲用戶輸入調用函數htmlspecialchars(),但始終在輸出爲HTML之前調用該函數。

3)在您的登錄處理程序中,您使用POST變量訪問一次密碼,並使用變量$ password訪問一次密碼。變量$ password僅在if語句中設置,所以如果輸入爲空,則填充錯誤,但您繼續使用未初始化的$ password變量。要麼只是在開始處填寫變量,要麼始終使用POST變量。

4)由於您使用的是PHP 5.3.29,因此您可以使用新函數password_hash()compatibility pack。我不認爲PHPass庫是這裏的問題,但這裏是新函數的一個例子。

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

5)另一種常發的錯誤是,用於存儲散列值數據庫字段太短,它需要VARCHAR(60)的長度。也許你可以提供你的一個密碼哈希(當然只是一個例子)?

0

這個庫需要PHP> = 5.3.7或者有一個$ 2y的修復版本(例如RedHat提供)。