Convert MD5 to base62 for URL

前端 未结 9 516
再見小時候
再見小時候 2020-12-30 10:43

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

I have read in many places that because the number from an MD5 is bigger than

9条回答
  •  遥遥无期
    2020-12-30 11:27

    You can do this like this: (Not all steps are in php, it's been a long time that I've used it.)

    • Create a md5 hash of the script like this:

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

    • Convert that number to base 62.

      See the questions about base conversion of arbitrary sized numbers in PHP

    • Truncate the string to a length you like.

    There's no risk in using only a few of the bits of a md5. All that changes is danger of collisions.

提交回复
热议问题