tags:

views:

511

answers:

4

I need to create a simple hashing method for passing some data in a URL. It doesn't need to be very secure, it just shouldn't be obvious to most people.

The hash needs to contains the numerical id of the sender and the id of the recipient and I should be able to decode the data after reading the appended hash.

Any ideas? I'd like the hash to be a short as possible, simply because this url is meant to be shared via IM, email, etc..

+7  A: 

Hash is one way only. If you want to decrypt it, you have to encrypt it. Try mcrypt with one of these.

For non secure stuff you can try base64_encode, You can also base_convert each numeric id from 10 to 36 digits or so. Multiplying the numbers with a secret constant could also help.

$obscurity = base_convert($recipientId * 42, 10, 36) . ':' . base_convert($senderId * 42, 10, 36)
OIS
+1  A: 

You could use encryption, as mentioned by @OIS.

Or you could use a hash and store the hash values in a database keyed to sender id and recipient id. PHP has md5() and sha1() built in.

Kalium
md5 and sha1 can't be (easily) decoded on the other end.
Alan Storm
Which is why he suggested storing the hash values in a database keyed to the sender and recipient ids. It may not be the best solution, but it would work. Upvoted to counter needless downvote.
Andrew
+2  A: 

Try base64 encoding/decoding. put together with the apache option "Multiviews" or apache mod_rewrite, would make your urls look like:

http://mysite.com/messages/[encoded string here]
Roy Rico
+2  A: 

base64 definitely should do the trick if you want to decode it again.

Note that this is not a 'hash', a hash generally means one-way encryption.

$senderId = 1234;
$recipientId = 5678;
$myString = $senderId . ":" . $recipientId;
echo base64_encode($myString);

Evert