note 90175 modified in function.sha1 by danbrown
| From: | danbrown@php.net | Date: | Wed, 08 Apr 2009 23:12:03 +0000 |
| Subject: | note 90175 modified in function.sha1 by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-152985@lists.php.net to get a copy of this message | ||
A function I created to create salted hash, dependant on password length.
This function creates a salted hash, and will embed the salt into the final hash so it can be
retrieved later for validation by means of the length of the password that is inputted.
Basically to validate, you need to take the length of the password and the amount of characters
created by encryption method to pull the salt out the full hash saved, this will get the salt, you
can then use this salt to check any pass against whats saved.
It uses the method of embedding the salt into the saved hash, the hash is placed into the salt pass
hash at a position that is determined by the password you input meaning the attacker never knows
your pass length so doesn't know where the random salt is placed in the password.
if this sounds confusing, it is..............
<?php
// function to obscure passwords //
function createHash($inText, $salt=NULL, $mode='sha1'){
// set where salt will appear in hash //
$saltStart = (strlen($inText)/2)+1;
// split the password by the number above //
$splitPass = str_split($inText, $saltStart);
// if no salt given create random one //
if($salt == NULL) {
$salt = hash($mode, time());
}
// add salt into the password and hash it //
if(count($splitPass) < 2) {
$outHash = hash($mode, $salt.$splitPass[0]);
} else {
$outHash = hash($mode, implode($salt, $splitPass));
}
// place salt inside the password hash at password length position //
// find location for hiding salt by password length //
if((strlen($inText)) > 18) {
$hideHash = 19;
} else {
$hideHash = strlen($inText)+1;
}
// split pass and salt hash by pass length //
$hashStart = substr($outHash,0,$hideHash);
$hashEnd = substr($outHash, $hideHash);
// put salt into hash //
$saltHash = $hashStart.$salt.$hashEnd;
return $saltHash;
}
// end ///////////////////////////
?>
--was--
A function I created to create salted hash, dependant on password length.
This function creates a salted hash, and will embed the salt into the final hash so it can be
retrieved later for validation by means of the length of the password that is inputted.
Basically to validate, you need to take the length of the password and the amount of characters
created by encryption method to pull the salt out the full hash saved, this will get the salt, you
can then use this salt to check any pass against whats saved.
It uses the method of embedding the salt into the saved hash, the hash is placed into the salt pass
hash at a position that is determined by the password you input meaning the attacker never knows
your pass length so doesn't know where the random salt is placed in the password.
if this sounds confusing, it is..............
// function to obscure passwords //
function createHash($inText, $salt=NULL, $mode='sha1'){
// set where salt will appear in hash //
$saltStart = (strlen($inText)/2)+1;
// split the password by the number above //
$splitPass = str_split($inText, $saltStart);
// if no salt given create random one //
if($salt == NULL) {
$salt = hash($mode, time());
}
// add salt into the password and hash it //
if(count($splitPass) < 2) {
$outHash = hash($mode, $salt.$splitPass[0]);
} else {
$outHash = hash($mode, implode($salt, $splitPass));
}
// place salt inside the password hash at password length position //
// find location for hiding salt by password length //
if((strlen($inText)) > 18) {
$hideHash = 19;
} else {
$hideHash = strlen($inText)+1;
}
// split pass and salt hash by pass length //
$hashStart = substr($outHash,0,$hideHash);
$hashEnd = substr($outHash, $hideHash);
// put salt into hash //
$saltHash = $hashStart.$salt.$hashEnd;
return $saltHash;
}
// end ///////////////////////////
http://php.net/manual/en/function.sha1.php