benshepherd icon

sha512 class hashing and comparing

benshepherd | PRO | 01/30/14 08:08:27 PM UTC | 0 ⭐ | 282 👁️ | Never ⏰ | []
PHP |

960 B

|

None

|

0 👍

/

0 👎

<?php
 
    class Crypt
    {
        //Split hash
        private static $delimter = '`';
        
        //Private salt
        private static $private_salt = 'cc1fb772da1843b2c43731360ef';
        
        //Compare password with another hash
        public static function compare($password, $compare_hash)
        {
            if(strstr($compare_hash, self::$delimter))
            {
                $split = explode(self::$delimter, $compare_hash);
                $salt = $split[0];
                $created_hash = self::create($password, $salt);
                
    
                return ($created_hash == $compare_hash);
            }
            
            return FALSE;
        }       
        
        //Create password with generated salt or own supplied
        public static function create($password, $salt = FALSE)
        {
            if(!$salt)
            {
                $salt = self::make_salt();  
            }
            return $salt.self::$delimter.hash('sha512',$password.$salt.self::$private_salt);
        }
        
        //Create a random salt
        public static function make_salt()
        {
            return substr(str_shuffle(sha1(microtime().rand())),0,16);
        }
    }

Comments