encode data in md5 (or similar)

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
GeXus
Forum Regular
Posts: 631
Joined: Sat Mar 11, 2006 8:59 am

encode data in md5 (or similar)

Post by GeXus »

I'd like to take two values, one a datetime stamp and one a unique id (which could range from 1-100,000)... and encode them into a hash of some kind.... ideally the shorter the better.... would anyone have suggestions for the best way to encode this in php? so that later i can decode it.
EricS
Forum Contributor
Posts: 183
Joined: Thu Jul 11, 2002 12:02 am
Location: Atlanta, Ga

Post by EricS »

The final size of the hash is completely dependent on the hashing algorithm. The final hash size for any amount of data being hashed changes very little (only by a character or two) for most hashing algorithms. So md5 for instance. If you hash 100MBs of data or 10 KBs of data, the resulting hash size is still a 32 character hexidecimal value.

As for how to hash this using MD5:

Code: Select all

$hash = md5($timestamp . $unique_id);
Edit: Opps I missed the very last sentence. Hashes can not be decoded later. You can only rehash values and compare the hashes for equality. If decoding is what you want. Then encryption (not hashing) is what you seek. Look up Blowfish, AES, DES for examples.
cade
Forum Commoner
Posts: 55
Joined: Tue Jul 03, 2007 8:18 pm

Post by cade »

hey guys...it's good news for me today. The coldfusion function for hash works the same as our md5. This is really good news
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post by onion2k »

cade wrote:hey guys...it's good news for me today. The coldfusion function for hash works the same as our md5. This is really good news
If you hash data you will not be able to 'unhash' it. A hashing algorithm is always one way only. You want an encryption algorithm instead if you want to be able to get the original data back again.

You should also be aware that MD5 is somewhat flawed. It's not really a big deal for most people, but there are better alternatives around (for PHP anyway (SHA256), I can't speak for ColdFusion).
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Re: encode data in md5 (or similar)

Post by superdezign »

GeXus wrote:would anyone have suggestions for the best way to encode this in php? so that later i can decode it.
You seem to have confused hashing with encryption, as onion has stated. Both PHP and MySQL have built-in encryption algorithms, but from the sound of what you are doing, you probably don't *actually* need one. In fact, you could just convert back and forth from hexadecimal and decimal if you must shorten the string.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Re: encode data in md5 (or similar)

Post by feyd »

superdezign wrote:In fact, you could just convert back and forth from hexadecimal and decimal if you must shorten the string.
Decimal is often quite longer actually, and varies in length. Binary however is always half the length of the hex string of a hash.
Post Reply