Manixat Posted March 19, 2013 Share Posted March 19, 2013 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 ? Quote Link to comment Share on other sites More sharing options...
DaveyK Posted March 19, 2013 Share Posted March 19, 2013 All one way algorythms are identifiable by their original text, otherwise they have no use. I would personnally always use crypt(), but you wont make it to 8 characters. WHy 8 characters? Quote Link to comment Share on other sites More sharing options...
Manixat Posted March 19, 2013 Author Share Posted March 19, 2013 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. Quote Link to comment Share on other sites More sharing options...
Zane Posted March 19, 2013 Share Posted March 19, 2013 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. Quote Link to comment Share on other sites More sharing options...
Zane Posted March 19, 2013 Share Posted March 19, 2013 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. Quote Link to comment Share on other sites More sharing options...
Manixat Posted March 19, 2013 Author Share Posted March 19, 2013 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 :/ Quote Link to comment Share on other sites More sharing options...
PaulRyan Posted March 19, 2013 Share Posted March 19, 2013 (edited) 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 March 19, 2013 by PaulRyan Quote Link to comment Share on other sites More sharing options...
Manixat Posted March 19, 2013 Author Share Posted March 19, 2013 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. Quote Link to comment Share on other sites More sharing options...
Psycho Posted March 19, 2013 Share Posted March 19, 2013 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. Quote Link to comment Share on other sites More sharing options...
Psycho Posted March 19, 2013 Share Posted March 19, 2013 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 Quote Link to comment Share on other sites More sharing options...
Manixat Posted March 19, 2013 Author Share Posted March 19, 2013 (edited) 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 March 19, 2013 by Manixat Quote Link to comment Share on other sites More sharing options...
Psycho Posted March 19, 2013 Share Posted March 19, 2013 (edited) 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 March 19, 2013 by Psycho Quote Link to comment Share on other sites More sharing options...
davidannis Posted March 19, 2013 Share Posted March 19, 2013 Why not use 4 characters that are sequential (0-9a-z) followed by four random characters. No need to check the database. No repeats. 36^4 combinations = 1,679,616. If you need more combinations add upper case or special characters. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.