note 96711 added to function.hash

From: Date: Fri, 12 Mar 2010 11:40:33 +0000
Subject: note 96711 added to function.hash
Groups: php.notes 
Request: Send a blank email to php-notes+get-166951@lists.php.net to get a copy of this message
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); } } ?> ---- Server IP: 64.71.164.2 Probable Submitter: 68.170.232.57 ---- Manual Page -- http://www.php.net/manual/en/function.hash.php Edit -- https://master.php.net/note/edit/96711 Del: integrated -- https://master.php.net/note/delete/96711/integrated Del: useless -- https://master.php.net/note/delete/96711/useless Del: bad code -- https://master.php.net/note/delete/96711/bad+code Del: spam -- https://master.php.net/note/delete/96711/spam Del: non-english -- https://master.php.net/note/delete/96711/non-english Del: in docs -- https://master.php.net/note/delete/96711/in+docs Del: other reasons-- https://master.php.net/note/delete/96711 Reject -- https://master.php.net/note/reject/96711 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#166951) next »