note 88057 added to function.sha1
| From: | mVamer at osu1 dot php dot net | Date: | Thu, 08 Jan 2009 01:49:42 +0000 |
| Subject: | note 88057 added to function.sha1 | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-148936@lists.php.net to get a copy of this message | ||
If I correctly understand what ranko84 is on about, this would be a simpler function with roughly
the same result.
<?php
function obscure($password, $algorythm = "sha1")
{
// Get some random salt.
$salt = hash($algorythm, uniqid(rand(), true));
// Determine the length of the hash.
$hash_length = strlen($salt);
// Determine the length of the password.
$password_length = strlen($password);
// Determine the maximum length of password. This is only needed if
// the user enters a very long password. In any case, the salt will
// be a maximum of half the end result. The longer the hash, the
// longer the password/salt can be.
$password_max_length = $hash_length / 2;
// Shorten the salt based on the length of the password.
if ($password_length >= $password_max_length)
{
$salt = substr($salt, 0, $password_max_length);
}
else
{
$salt = substr($salt, 0, $password_length);
}
// Determine the length of the salt.
$salt_length = strlen($salt);
// Determine the salted hashed password.
$salted_password = hash($algorythm, $salt . $password);
// If we add the salt to the hashed password, we would get a hash that
// is longer than a normally hashed password. We don't want that; it
// would give away hints to an attacker. Because the password and the
// length of the password are known, we can just throw away the first
// couple of characters of the salted password. That way the salt and
// the salted password together are the same length as a normally
// hashed password without salt.
$used_chars = ($hash_length - $salt_length) * -1;
$final_result = $salt . substr($salted_password, $used_chars);
return $final_result;
}
?>
----
Server IP: 213.249.64.162
Probable Submitter: 88.159.81.164
----
Manual Page -- http://www.php.net/manual/en/function.sha1.php
Edit -- https://master.php.net/note/edit/88057
Del: integrated -- https://master.php.net/note/delete/88057/integrated
Del: useless -- https://master.php.net/note/delete/88057/useless
Del: bad code -- https://master.php.net/note/delete/88057/bad+code
Del: spam -- https://master.php.net/note/delete/88057/spam
Del: non-english -- https://master.php.net/note/delete/88057/non-english
Del: in docs -- https://master.php.net/note/delete/88057/in+docs
Del: other reasons-- https://master.php.net/note/delete/88057
Reject -- https://master.php.net/note/reject/88057
Search -- https://master.php.net/manage/user-notes.php