note 96711 deleted from function.hash by danbrown

From: Date: Fri, 12 Mar 2010 13:16:10 +0000
Subject: note 96711 deleted from function.hash by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-166955@lists.php.net to get a copy of this message
Note Submitter: fyrye at torntech dot com ---- Here's an example Class of one way secure hashing with a random salt. Default $algo is whirlpool which does not currently have a reported crack and outputs a 512-bit (128 hexits) digest. Allows you to define the length of the salt to use, and returns with hexits or binary to match your hash preference. <?php /****************************************************** *credit to http://www.richardlord.net/blog/php-password-security *Modified to define salt length ******************************************************/ class Encrypt{ var $data = false, $hasSalt = false, $algo = 'whirlpool'; var $rawOut = false, $check = false, $len = 8; public function __construct() { $args = func_get_args(); if( $args[0] ) $this->data = (string)$args[0]; if( $args[1] ) $this->hasSalt = $args[1]; if( $args[2] ) $this->algo = $args[2]; if( $args[3] ) $this->blnVals = (bool)$args[3]; if( $args[4] ) $this->check = (string)$args[4]; if( $args[5] ) $this->len = (string)$args[5]; } //$myEnc = new Encrypt(); //$myEnc->salt(); public function salt(){ return substr(hash('md5', str_pad(mt_rand(), $this->len, '0', STR_PAD_LEFT), $this->rawOut), -$this->len); } //$myEnc = new Encrypt('yourData', false, false, false, 'EncryptedData'); //or //$myEnc = new Encrypt('yourData'); //$myEnc->check = 'EncryptedData'; // //$myEnc->compare() returns true or false public function compare(){ if(!$this->check) return false; if(!$this->hasSalt) $this->hasSalt = substr($this->check, 0, $this->len); return $this->check == $this->hash(); } //$myEnc = new Encrypt('yourData'); //or //$myEnc = new Encrypt(); //$myEnc->data = 'yourData'; // //$myEnc->hash() returns salt + encrypted data public function hash(){ if(!$this->hasSalt) $this->hasSalt = $this->salt(); return $this->hasSalt . hash($this->algo, $this->hasSalt.$this->data, $this->rawOut); } } ?>

« previous php.notes (#166955) next »