benshepherd

sha512 class hashing and comparing

Jan 30th, 2014
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.96 KB | None | 0 0
  1. <?php
  2.  
  3.     class Crypt
  4.     {
  5.         //Split hash
  6.         private static $delimter = '`';
  7.        
  8.         //Private salt
  9.         private static $private_salt = 'cc1fb772da1843b2c43731360ef';
  10.        
  11.         //Compare password with another hash
  12.         public static function compare($password, $compare_hash)
  13.         {
  14.             if(strstr($compare_hash, self::$delimter))
  15.             {
  16.                 $split = explode(self::$delimter, $compare_hash);
  17.                 $salt = $split[0];
  18.                 $created_hash = self::create($password, $salt);
  19.                
  20.    
  21.                 return ($created_hash == $compare_hash);
  22.             }
  23.            
  24.             return FALSE;
  25.         }      
  26.        
  27.         //Create password with generated salt or own supplied
  28.         public static function create($password, $salt = FALSE)
  29.         {
  30.             if(!$salt)
  31.             {
  32.                 $salt = self::make_salt(); 
  33.             }
  34.             return $salt.self::$delimter.hash('sha512',$password.$salt.self::$private_salt);
  35.         }
  36.        
  37.         //Create a random salt
  38.         public static function make_salt()
  39.         {
  40.             return substr(str_shuffle(sha1(microtime().rand())),0,16);
  41.         }
  42.     }
Advertisement
Add Comment
Please, Sign In to add comment