Advertisement
cafreak

PHP Password Hashing

Feb 11th, 2015
271
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.19 KB | None | 0 0
  1. <?php
  2.     /*-------------SimpleBB.net--------------\
  3.     |  Project Start Date 02-February-2015;  |
  4.     |  Project Developer Mike M;         |
  5.     |  Project Type: Ebay Linking;       |
  6.     \--------------SimpleBB.net-------------*/
  7.     class Account_Functions{
  8.        
  9.         function __Construct(){
  10.            
  11.         }
  12.        
  13.         /**
  14.          *   A method to generate randomized data [RDG | Random Data Generator] for example salting or session keys
  15.          *  @param integer $length DESCRIPTION: used to set a length that key should have
  16.          *  @param boolean $base64encode OPTIONAL DESCRIPTION: if true it will use base64_encode() on the return data
  17.          *  @param boolean $isSalt OPTIONAL DESCRIPTION: if true it will base64_encode() on the return data and will truncate the size to 22
  18.          *  @return string $value DESCRIPTION: returns the randomized data
  19.          */
  20.         function RDG($length, $base64encode = false, $isSalt = false){
  21.            
  22.             if(!is_integer($length)){
  23.                 trigger_error("RDG(): expects parameter 1 to be an integer, ".gettype($length)." given", E_USER_ERROR);
  24.             }
  25.            
  26.             if(!is_bool($base64encode)){
  27.                 trigger_error("RDG(): expects parameter 2 to be a boolean, ".gettype($base64encode)." given", E_USER_ERROR);
  28.             }
  29.            
  30.             if(!is_bool($isSalt)){
  31.                 trigger_error("RDG(): expects parameter 3 to be a boolean, ".gettype($base64encode)." given", E_USER_ERROR);
  32.             }
  33.            
  34.             if($isSalt){
  35.                 $base64encode = true;
  36.             }
  37.            
  38.             if(function_exists('mcrypt_create_iv')){
  39.                 $value = @mcrypt_create_iv($length, MCRYPT_DEV_URANDOM);
  40.                 if($value === ""){
  41.                     $value = @mcrypt_create_iv($length, MCRYPT_DEV_RANDOM);
  42.                     if($value === ""){
  43.                         trigger_error("MCRYPT_DEV_RANDOM and/or MCRYPT_DEV_URANDOM are/is not available", E_USER_ERROR);
  44.                     }
  45.                 }
  46.             }else if(function_exists('openssl_random_pseudo_bytes')){
  47.                 $value = openssl_random_pseudo_bytes($length);
  48.                 if($value === ""){
  49.                     trigger_error("openssl_random_pseudo_bytes() is not available", E_USER_ERROR);
  50.                 }
  51.  
  52.             }else{
  53.                 trigger_error("mcrypt_create_iv() and openssl_random_pseudo_bytes() are not available", E_USER_ERROR);
  54.             }
  55.            
  56.             if($base64encode){
  57.                 $value = rtrim(base64_encode($value), "=");
  58.                 $value = str_replace("+", ".", $value);
  59.             }
  60.            
  61.             if($isSalt){
  62.                 $value = substr($value, 0, 22);
  63.                 if(strlen($value) <> 22){
  64.                     trigger_error("The salt length must be 22", E_USER_ERROR);;
  65.                 }
  66.             }
  67.            
  68.             return $value;
  69.         }
  70.    
  71.         function pw_hash($password, $cost = 12){
  72.             if(!function_exists('crypt')){
  73.                 trigger_error("crypt() is not available", E_USER_ERROR);
  74.             }
  75.        
  76.             if(!is_string($password)){
  77.                 trigger_error("pw_hash(): expects parameter 1 to be a string,", E_USER_ERROR);
  78.             }
  79.            
  80.             if(!is_integer($cost)){
  81.                 trigger_error("pw_hash(): expects parameter 2 to be an integer, ".gettype($cost)." given", E_USER_ERROR);
  82.             }
  83.            
  84.             if($cost < 9){
  85.                 trigger_error("pw_hash(): expects value of parameter 2 to be greater than 8, ".$cost." given", E_USER_ERROR);
  86.             }
  87.            
  88.             if($cost < 9){
  89.                 trigger_error("pw_hash(): expects value of parameter 2 to be greater than 8, ".$cost." given", E_USER_ERROR);
  90.             }
  91.            
  92.             if($cost > 31){
  93.                 trigger_error("pw_hash(): expects value of parameter 2 to be smaller than 32, ".$cost." given", E_USER_ERROR);
  94.             }
  95.            
  96.            
  97.             if(strlen($password) < 7 || empty($password)){
  98.                 trigger_error("pw_hash(): expects value of parameter 1 to be greater than 7".strlen($password)." characters given", E_USER_ERROR);
  99.             }
  100.  
  101.             $salt = SELF::RDG(22, true, true);
  102.            
  103.             if(version_compare(phpversion(), '5.3.7', '<')){
  104.                 $salt = '$2a$'.$cost.'$'.$salt;
  105.             }else{
  106.                 $salt = '$2y$'.$cost.'$'.$salt;
  107.             }
  108.            
  109.             $hashed = crypt($password, $salt);
  110.            
  111.            
  112.             if(strlen($hashed) <> 60){
  113.                 trigger_error("pw_hash(): Returned password is not valid", E_USER_ERROR);
  114.             }
  115.            
  116.             return array("Password" => $hashed, "Salt" => $salt, "Cost" => $cost);
  117.            
  118.            
  119.         }
  120.     }
  121.    
  122.     for($i = 0; $i < 20; $i++){
  123.         var_dump(Account_Functions::pw_hash("{insert_password#@$%+=_here.}"));
  124.         echo("<hr />");
  125.     }
  126.    
  127.    
  128.     /*-------------SimpleBB.net--------------\
  129.     |  Project Start Date 02-February-2015;  |
  130.     |  Project Developer Mike M;         |
  131.     |  Project Type: Ebay Linking;       |
  132.     \--------------SimpleBB.net-------------*/
  133.  
  134. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement