Guest User

Untitled

a guest
Nov 19th, 2018
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.03 KB | None | 0 0
  1. <?php
  2.  
  3. $username = 'Admin';
  4. $password = 'gf45_gdf#4hg';
  5.  
  6. // A higher "cost" is more secure but consumes more processing power
  7. $cost = 10;
  8.  
  9. // Create a random salt
  10. $salt = strtr(base64_encode(mcrypt_create_iv(16, MCRYPT_DEV_URANDOM)), '+', '.');
  11.  
  12. // Prefix information about the hash so PHP knows how to verify it later.
  13. // "$2a$" Means we're using the Blowfish algorithm. The following two digits are the cost parameter.
  14. $salt = sprintf("$2a$%02d$", $cost) . $salt;
  15.  
  16. // Value:
  17. // $2a$10$eImiTXuWVxfM37uY4JANjQ==
  18.  
  19. // Hash the password with the salt
  20. $hash = crypt($password, $salt);
  21.  
  22. // Value:
  23. // $2a$10$eImiTXuWVxfM37uY4JANjOL.oTxqp7WylW7FCzx2Lc7VLmdJIddZq
  24.  
  25.  
  26.  
  27.  
  28. // Verify
  29. $username = 'Admin';
  30. $password = 'gf45_gdf#4hg';
  31.  
  32. $sth = $dbh->prepare('
  33. SELECT
  34. hash
  35. FROM users
  36. WHERE
  37. username = :username
  38. LIMIT 1
  39. ';
  40.  
  41. $sth->bindParam(':username', $username);
  42.  
  43. $sth->execute();
  44.  
  45. $user = $sth->fetch(PDO::FETCH_OBJ);
  46.  
  47. // Hashing the password with its hash as the salt returns the same hash
  48. if ( crypt($password, $user->hash) == $user->hash ) {
  49. // Ok!
  50. }
Add Comment
Please, Sign In to add comment