note 52365 added to function.sha1
| From: | alex at milivojevic dot org | Date: | Thu, 28 Apr 2005 17:45:54 +0000 |
| Subject: | note 52365 added to function.sha1 | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-88709@lists.php.net to get a copy of this message | ||
If you don't have mhash library and/or PHP module (for example, Red Hat systems, which includes
Fedora), and you don't feel like adding it, you can use something like this to generate and
verify SSHA hashes.
<?php
$password = "test";
// Generate SSHA hash
mt_srand((double)microtime()*1000000);
$salt = pack("CCCC", mt_rand(), mt_rand(), mt_rand(), mt_rand());
$hash = "{SSHA}" . base64_encode(pack("H*", sha1($password . $salt)) . $salt);
echo $hash . "\n";
// Verify SSHA hash
$ohash = base64_decode(substr($hash, 6));
$osalt = substr($ohash, 20);
$ohash = substr($ohash, 0, 20);
$nhash = pack("H*", sha1($password . $osalt));
if ($ohash == $nhash) {
echo "Password OK\n";
} else {
echo "Password verifiaction failed\n";
}
?>
----
Manual Page -- http://www.php.net/manual/en/function.sha1.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+52365
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+52365&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+52365&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+52365&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+52365&report=yes&reason=useless
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+52365&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+52365&report=yes
Search -- http://master.php.net/manage/user-notes.php