|
Posted by amygdala on 04/15/07 22:32
"amygdala" <noreply@noreply.com> schreef in bericht
news:462293f6$0$3240$9a622dc7@news.kpnplanet.nl...
> Hi,
>
> Does anybody now of a custom crypt function that implements sha1? The
> thing I like about crypt is that I don't have to worry about
> (re)generating salt when querying the database. Or are there perhaps other
> functions that implements this same functionality and generates a sha1
> hash?
>
> Cheers.
Sorry, never mind, I found one at:
http://nl3.php.net/crypt
In one of the user comments.
From what I can tell, with my limited experience, it does the job
appropriately. If you beg to differ, please don't hold back.
Here's the code for those interested in it:
<?php
// NOTE: This function requires PHP 5.0.0 as we use "raw output" option of
sha1()
function sha1crypt($password, $salt=null) {
if ( (is_null($salt)) || (strlen($salt)<1) ) {
$salt='';
while(strlen($salt)<10) $salt.=chr(rand(64,126));
$salt='$sha1$'.$salt.'$';
}
if ($salt{0}!='$') return crypt($password, $salt);
$tmp=explode('$',$salt);
if ($tmp[1]!='sha1') return crypt($password, $salt);
$saltstr=$tmp[2];
if (strlen($saltstr) != 10) return crypt($password, $salt);
$encrypt=base64_encode(sha1($saltstr.$password,true));
return '$sha1$'.$saltstr.'$'.$encrypt;
}
// without salt, sha1crypt() will generate on
$pass=sha1crypt('foobar');
echo $pass."\n";
// pass directly password as salt - different output as password is not the
same
echo sha1crypt('foobarbaz',$pass)."\n";
// same password - same output
echo sha1crypt('foobar',$pass)."\n";
// Encrypt using MD5 passwords
echo sha1crypt('foobar','$1$blahblahg$')."\n";
?>
Navigation:
[Reply to this message]
|