• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1445
  • Last Modified:

Convert md5 hash to contain ALL letters

I noticed that md5() seems to only return hexadecimal values.

Is there a way to convert that to something that contains letters all the way to z?

If not is there another similar hash that can do this?
0
hankknight
Asked:
hankknight
2 Solutions
 
Michael701Commented:
I guess you'd take the decimal value, then convert to base 26 (assigning the results to a-z)

but why?
0
 
hernst42Commented:
md5 only uses hex-chars because it's the most easiest way to encode and decode 128bits. You can write you own function to convert the 8-byte (128bit) String in any other encoding you like, but it's a lot of bit juggeling. You could use http://www.php.net/manual/en/function.convert-uuencode.php or http://www.php.net/manual/en/function.base64-encode.php which pack more bits into a single char
0
 
BenMorelCommented:
Hi,
You need to base-convert the md5() function result, from a 16-chars base (md5 hex) to a 62-chars base (0-9 a-z A-Z).
Since php's built-in functions still rely on approximative float (double) types to do that, I use custom-made functions based on the bc lib (should be available in most php builds).
At the very bottom, a function CustomMD5() you can use instead of md5().
Regards,
Ben
<?php
define('HEX_CHARS',    '0123456789abcdef');
define('BASE62_CHARS', '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ');
 
/*********************************************/
function ConvertFromArbitraryBase($Str, $Chars)
/*********************************************/
{
    /*
        Converts from an arbitrary-base string to a decimal string
    */
    
    if (ereg('^[' . $Chars . ']+$', $Str))
    {
        $Result = '0';
        
        for ($i=0; $i<strlen($Str); $i++)
        {
            if ($i != 0) $Result = bcmul($Result, strlen($Chars));
            $Result = bcadd($Result, strpos($Chars, $Str[$i]));
        }
        
        return $Result;
    }
    
    return false;
}
 
/*******************************************/
function ConvertToArbitraryBase($Str, $Chars)
/*******************************************/
{
    /*
        Converts from a decimal string to an arbitrary-base string
    */
 
    if (ereg('^[0-9]+$', $Str))
    {
        $Result = '';
        
        do
        {
            $Result .= $Chars[bcmod($Str, strlen($Chars))];
            $Str = bcdiv($Str, strlen($Chars));
        }
        while (bccomp($Str, '0') != 0);
        
        return strrev($Result);
    }
    
    return false;
}
 
/**********************/
function CustomMD5($Str)
/**********************/
{
	return ConvertToArbitraryBase(ConvertFromArbitraryBase(md5($Str), HEX_CHARS), BASE62_CHARS);
}
?>

Open in new window

0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now