Jump to content

Short encoding/encrypting ?


Manixat

Recommended Posts

Hello freaks.

 

I'm in a need of a function with the exact same functionality as sha1, md5 and such except I need the encrypted string to be no longer than 8 characters, as well as I need it to be identifiable by encoding the original text again. Is there something like that ?

Link to comment
Share on other sites

I need to send a code as an SMS ( or text message ) and the user has to type it in to activate certain sections of the website, but a code only lasts a week, so next week the user has to send another SMS and having my users typing in 32 characters of code every week is not going to be pleasant.

Link to comment
Share on other sites

I need to send a code as an SMS ( or text message ) and the user has to type it in to activate certain sections of the website, but a code only lasts a week, so next week the user has to send another SMS and having my users typing in 32 characters of code every week is not going to be pleasant.

 

If that is all you need it for then disregard my integrity statement.

Link to comment
Share on other sites

If you only want 8 characters then use substr

echo substr( md5("password"), 0, ;



Keep in mind though, this will lower the integrity of your hashes by a long shot.

 

I thought of that and at some point, I believe, the beginning ( 8 characters ) will repeat some other hash, which is a different code but starts the same and I really don't want that to happen :/

Link to comment
Share on other sites

Something like this?

<?PHP
  function smsCode() {
    $string = '';  
      
    for($i=0; $i<8; $i++) {
      $string .= mt_rand(1,100);        
    }
    
    return substr(sha1($string),mt_rand(0,23),;
  }
 
  echo smsCode();
?>
Edited by PaulRyan
Link to comment
Share on other sites

I thought of that and at some point, I believe, the beginning ( 8 characters ) will repeat some other hash, which is a different code but starts the same and I really don't want that to happen :/

 

Every HASH will repeat at some point! Even a 32 bit hash will repeat because the number of things that can be hashed is infinite and the number of hashes is finite. If you are not having 10's of thousands of users then using a substr of a hash should work just fine.

Link to comment
Share on other sites

I thought of that and at some point, I believe, the beginning ( 8 characters ) will repeat some other hash, which is a different code but starts the same and I really don't want that to happen :/

 

 

Exactly, but I need a way to ensure that codes will never duplicate. I suppose I could check a code if it exists in the db before sending it out but that's what I'll do if there's no other (simpler) way.

 

 

Every HASH will repeat at some point! Even a 32 bit hash will repeat because the number of things that can be hashed is infinite and the number of hashes is finite. If you are not having 10's of thousands of users to manage with these codes a substr() of a normal hash should do just fine

Link to comment
Share on other sites

Every HASH will repeat at some point!

 

That's not nice to hear..

 

I ran PaulRyan's suggestion through a simple 1000 codes check and it repeated 7 times. I guess I'll be doing it the old fashion way

 

EDIT: I just gave it about 10 more runs and it didn't repeat even once .. tough luck

Edited by Manixat
Link to comment
Share on other sites

That's not nice to hear..

 

I ran PaulRyan's suggestion through a simple 1000 codes check and it repeated 7 times. I guess I'll be doing it the old fashion way

 

EDIT: I just gave it about 10 more runs and it didn't repeat even once .. tough luck

 

Um, yeah, that's to be expected. A hash has a VERY large number of possible combinations. So a measly 1000 codes would almost certainly not have a duplicate. I ran a test generating the 8 character substring of the MD5() hash for each number from 1 to 50,000 and there were no duplicates. But, again, that is to be expected. But, to make a statement that you want an 8 character "hash" that could NEVER have a duplicate is ridiculous.

 

Assuming you don't think you will need 50,000 codes, just have an incremental value in the DB and take the first 8 characters of the hash on that value. I would have tested it out further, but the script timed out and I'm not willing to invest additional time.

Edited by Psycho
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.