Connecting Tech Pros Worldwide Forums | Help | Site Map

crypt with sha1?

amygdala
Guest
 
Posts: n/a
#1: Apr 15 '07
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.



amygdala
Guest
 
Posts: n/a
#2: Apr 15 '07

re: crypt with sha1?



"amygdala" <noreply@noreply.comschreef in bericht
news:462293f6$0$3240$9a622dc7@news.kpnplanet.nl...
Quote:
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.
BTW: My system (localhost) implements md5. And I would rather not rely on a
future hosting server to have sha1 implemented.


amygdala
Guest
 
Posts: n/a
#3: Apr 15 '07

re: crypt with sha1?



"amygdala" <noreply@noreply.comschreef in bericht
news:462293f6$0$3240$9a622dc7@news.kpnplanet.nl...
Quote:
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,tru e));
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";

?>


Closed Thread