Guest User

PHP Hash test for Tweakers.net

a guest
Aug 6th, 2012
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.91 KB | None | 0 0
  1. <?php
  2. function tryLogin($user, $username, $password)
  3. {
  4.     $encryptions = hash_algos(); // 2 = md5
  5.  
  6.     if($username == $user['username'])
  7.     {
  8.         $hash = explode('$', $user['password']);
  9.         array_shift($hash);
  10.         list($encIndex, $salt, $passwordhash) = $hash;
  11.         $encryption = $encryptions[$encIndex]; // md5
  12.         $newPassword = '';
  13.         if($encryption == 'md5')
  14.             $newPassword = md5($salt . $password);
  15.         $newHash = '$' . $encIndex . '$' . $salt . '$' . $newPassword;
  16.         echo $newHash . '<br />';
  17.         if($user['password'] == $newHash)
  18.             return true;
  19.     }
  20.     return false;
  21. }
  22.  
  23. function createUser($username, $password)
  24. {
  25.     $salt = uniqid();
  26.     $hash = '$2$' . $salt . '$' . md5($salt . $password);
  27.     return array('username' => $username, 'password' => $hash);
  28. }
  29.  
  30. $user = createUser('Erwin', 'Erwin');
  31. $wrong = tryLogin($user, 'Erwin', 'Erwintje'); // boolean false
  32. $good = tryLogin($user, 'Erwin', 'Erwin'); // boolean true
  33. ?>
Advertisement
Add Comment
Please, Sign In to add comment