crypt with sha1?

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View

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?


Re: crypt with sha1?

Quoted text here. Click to load it

BTW: My system (localhost) implements md5. And I would rather not rely on a  
future hosting server to have sha1 implemented.  

Re: crypt with sha1?

Quoted text here. Click to load it

Sorry, never mind, I found one at:
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:


// NOTE: This function requires PHP 5.0.0 as we use "raw output" option of  
function sha1crypt($password, $salt=null) {
  if ( (is_null($salt)) || (strlen($salt)<1) ) {
    while(strlen($salt)<10) $salt.=chr(rand(64,126));
  if ($salt!='$') return crypt($password, $salt);
  if ($tmp[1]!='sha1') return crypt($password, $salt);
  if (strlen($saltstr) != 10) return crypt($password, $salt);
  return '$sha1$'.$saltstr.'$'.$encrypt;

// without salt, sha1crypt() will generate on
echo $pass."\n";

// pass directly password as salt - different output as password is not the  
echo sha1crypt('foobarbaz',$pass)."\n";

// same password - same output
echo sha1crypt('foobar',$pass)."\n";

// Encrypt using MD5 passwords
echo sha1crypt('foobar','$1$blahblahg$')."\n";


Site Timeline