note 113526 deleted from function.hash by dm
| From: | dm@php.net | Date: | Tue, 29 Apr 2014 19:13:07 +0000 |
| Subject: | note 113526 deleted from function.hash by dm | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-199373@lists.php.net to get a copy of this message | ||
Note Submitter: Anonymous
----
I use this code for Secure Password Hashes
<?php
class SecurLoginHashing {
/**
* System Salt
* @var string
*/
protected $salt = '';
/**
* Hashing Iterations >= 1
* @var int
*/
protected $iterations = 2;
/**
* Hashing algorithm used each iteration
* @var string[]
*/
protected $algorithms = array();
/**
* @param string $salt
* @param int $iterations [Default: 2]
* @param string[] $algorithms [Default: array('sha512', 'ripemd320',
'whirlpool')]
*/
public function __construct( $salt, $iterations = 2, $algorithms = array('sha512',
'ripemd320', 'whirlpool')) {
$this->salt = $salt;
$this->iterations = $iterations;
$this->algorithms = $algorithms;
}
/**
* Generating a Safe Hash in Obj. Context
* @param string $toHash
* @param string $user_salt
* @return string
*/
public function hash($toHash, $user_salt){
return self::staticHash($toHash, $user_salt, $this->salt, $this->iterations,
$this->algorithms);
}
/**
* Generating a Safe Hash in static Context
* @param string $toHash
* @param string $user_salt
* @param string $salt
* @param int $iterations [Default: 2]
* @param string[] $algorithms [Default: array('sha512', 'ripemd320',
'whirlpool')]
* @return string
*/
public static function staticHash($toHash, $user_salt, $salt, $iterations = 2, $algorithms =
array('sha512', 'ripemd320', 'whirlpool')) {
$i=0;
do {
// Salting
$toHash = $user_salt.$toHash.$salt;
// Avoid Collision Attacks with different algorithms
$tempHash = '';
foreach($algorithms as $algo) {
$tempHash .= \hash($algo, $toHash);
}
// Reset Hash length
$toHash = \hash($algo, $tempHash);
$i++;
} while ($i<$iterations);
return $toHash;
}
}
?>