By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,154 Members | 1,043 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,154 IT Pros & Developers. It's quick & easy.

crypt with sha1?

P: n/a
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.
Apr 15 '07 #1
Share this Question
Share on Google+
2 Replies


P: n/a

"amygdala" <no*****@noreply.comschreef in bericht
news:46**********************@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.
BTW: My system (localhost) implements md5. And I would rather not rely on a
future hosting server to have sha1 implemented.
Apr 15 '07 #2

P: n/a

"amygdala" <no*****@noreply.comschreef in bericht
news:46**********************@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,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";

?>
Apr 15 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.