Shorter hash strings without compromising security [duplicate]
Possible Duplicate:
php short hash
MD5 hashes are long and inconvenient to use. How can I further encode a md5 string to produce a shorter string using a subset of characters, for example a-z, A-Z and 0-9? jsfiddle.net is doing som开发者_如何学编程ething like this on their website to produce short links easy to copy and paste and look like this: http://jsfiddle.net/uY7Pk/
- Generate a random N-character string.
- See if anything else already has that string as its shorturl in the database.
- If yes, go to 1. If no, store that string as the shorturl for the resource in the database.
There's no need to use hashing for url shorteners when you have a persistent datastore, because you're not actually encoding the long url, you're just associating a token with it.
JSFiddle is using an algorithm to shorten the URL, that has not much to do with MD5 hashing.
Since hash algorithms usually generate a certain length of Hex Data...
echo md5("Hello World");
-> b10a8db164e0754105b7a99be72e3fe5
...there might be algorithms to compress those hashes slightly further, but they will certainly result in a binary blob - which ain't a specific subset of characters, for example a-z, A-Z and 0-9.
Sidenote: PHPass is currently a solid way to use hashes in php.
I think you're getting confused between encryption and URL-shortening.
What you see on JSfiddle isn't encryption, it's a unique id that refers to the page. Instead of using solely numerical IDs, they're using letters too which allows many more combinations from the same number of characters.
use base64_encode($hash). It will produce an encode version which usualy is shorter.
Another way is to compute a crc32 which is a 32bit integer
But it depends on whether you need to recover your have afterwards.
精彩评论