Convert MD5 to base62 for URL

I have a script to convert to base 62 (A-Za-z0-9), but how do I get the number from MD5?

I read in many places that since a number from MD5 is larger than php can be processed as an integer, it will be inaccurate ... Since I want a short URL anyway and did not plan to use the entire hash, maybe only 8 characters ....

So my question is how to get part of the number of MD5 hashes?

Is it also nice to use only part of the MD5 hash?

+6
php md5 base62
source share
8 answers

I am going to propose another matter here. Since you're only interested in using the decimal fragment of the md5 hash, why don't you use some other short hash like CRC32 or Adler ? Here is an example:

$hash = sprintf('%u', crc32('your string here')); 

This will result in an 8 digit hash of your string.

EDIT: I think I misunderstood you. Here are some functions that provide conversions to and from databases to 62 .

EDIT (again) . To work with arbitrary length numbers, you must use either bc_math or the GMP extension, here which uses the bc_math extension, and can also convert from base 2 to base 62 . You should use it as follows:

 echo bc_base_convert(md5('your url here'), 16, 62); // public base 62 hash 

and reverse:

 echo bc_base_convert('base 62 encoded value here', 62, 16); // private md5 hash 

Hope this helps. =)

+6
source share

If possible, I would advise against using a hash for your URLs. In the end, you will run into conflicts ... especially if you truncate the hash. If you move forward and implement an identifier-based system, where each element has a unique identifier, there will be much less headaches. The first element will be 1 , the second will be 2 , etc. --- If you are using MySQL, just enter the auto-increment column.

To make a short identifier:

 //the basic example $sid = base_convert($id, 10, 36); //if you're going to be needing 64 bit numbers converted //on a 32 bit machine, use this instead $sid = gmp_strval(gmp_init($id, 10), 36); 

To make a short identifier back to base-10 id:

 //the basic example $id = base_convert($id, 36, 10); //if you're going to be needing 64 bit numbers //on a 32 bit machine, use this instead $id = gmp_strval(gmp_init($shortid, 36)); 

Hope this helps!

If you really need base 62 (which is not possible with gmp or base_convert ), check this out: http://snipplr.com/view/22246/base62-encode--decode/

+3
source share

You can do it like this: (Not all steps in php, I used this for a long time.)

  • Create the md5 script hash file as follows:

    $ hash = md5 (script, raw_output = true);

  • Convert this number to base 62.

    See questions about basic arbitrary size conversion in PHP

  • Truncate the string to the required length.

There is no risk of using just a few bits of md5. All that changes is the danger of collisions.

+1
source share

You can use a slightly modified Base 64 with - and _ instead of + and / :

 function base64_url_encode($str) { return strtr(base64_encode($str), array('+'=>'-', '/'=>'_')); } function base64_url_decode($str) { return base64_decode(strtr($str, array('-'=>'+', '_'=>'/'))); } 

Alternatively, you can remove the scroll symbols = .

And to get the original MD5 value (binary string), set the second parameter (named $raw_output in the manual) to true:

 $raw_md5 = md5($str, true); 
0
source share

You can do something like this,

 $hash = md5("The data to be hashed", true); $ints = unpack("L*num", $hash); $hash_str = base62($ints['num1']) . base62($ints['num2']) . base62($ints['num3']) . base62($ints['num4']) 
0
source share

Actually there is a Java implementation that you could extract. This is an open source CMS called Pulse.

Check out the code toBase62() and fromBase62() .

http://pulse.torweg.org/javadoc/src-html/org/torweg/pulse/util/StringUtils.java.html

The only dependency in StringUtils is the LifeCycle class, which provides a way to get a salt hash for a string that you can even omit together or simply copy the method into your copy of StringUtils . Voila.

0
source share

Starting with PHP 5.3.2, GMP supports databases up to 62 (previously there were only 36), so the brianreavis proposal was very close. I think the easiest answer to your question is:

 function base62hash($source, $chars = 22) { return substr(gmp_strval(gmp_init(md5($source), 16), 62), 0, $chars); } 

The conversion from base-16 to base-62 obviously has advantages in space. A typical 128-bit MD5 hash is 32 characters in hexadecimal, but in base-62 it is only 22. If you store hashes in a database, you can convert them to a raw binary file and save even more space (16 bytes for MD5 )

Since the resulting hash is just a string representation, you can just use substr if you only need a little bit of it (as the function does).

0
source share

You can try base62x to get a secure and compatible encoded representation.

Here is for more information on base62x or just -base62x in -NatureDNS .

 shell> ./base62x -n 16 -enc 16AF 1Ql shell> ./base62x -n 16 -dec 1Ql 16AF shell> ./base62x Usage: ./base62x [-v] [-n <2|8|10|16|32>] <-enc|dec> string Version: 0.60 
0
source share

All Articles