This is the little code that I made for hashing passwords for store in db.
- Code: Select all
<?php
class PasswordHash {
//Edit this salt value whit your own random salt
private static $salt = 'IAS(2801idji1dj-3idhJOHCJUH#&DF!)*@_DOIJKDJAEKDJFO';
public static function hash($password, $iterates = 10, $method = 'sha512', $compress = true, $compressMethod = 'md5') {
if ($compress) {
return hash($compressMethod, self::blow($password, $iterates, $method));
} else {
return self::blow($password, $iterates, $method);
}
}
public static function check($password, $hash, $method = 'sha512', $iterates = 10, $compress = true, $compressMethod = 'md5') {
if($compress){
if(hash($compressMethod, self::blow($password, $iterates, $method)) != $hash){
return false;
}
} else {
if(self::blow($password, $iterates, $method) != $hash){
return false;
}
}
}
protected static function blow($password, $iterates, $method) {
for ($i = 0; $i <= $iterates; $i++) {
$output = hash($method, isset($output) ? $password . self::$salt . $output : $password . self::$salt);
}
return $output;
}
}
You can save this code on your class folder. Now you can use the code:
- Code: Select all
Doo::loadClass('PasswordHash');
$hash = PasswordHash::hash('some_password');
//Now is safe to store $hash in you db
if(!PasswordHash::check('some_passoword', 'db_stored_hash')){
echo 'You have entered the wrong password!';
}
That's it!
Hope you guyz like this little code!
