fork download
  1. define('CRYPT_ALPHA','./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz');
  2.  
  3. /*
  4.  * Takes a 22 byte crypt-base-64 hash and converts it to base 16
  5.  * If the input is longer than 22 chars (e.g., the entire output of crypt()),
  6.  * then this function will strip all but the last 22. Fails if under 22 chars
  7.  *
  8.  * @param string $hash The hash to convert
  9.  * @param string The equivalent base16 hash (therefore a number)
  10.  */
  11. function md5_b64tob16($hash) {
  12. if (strlen($hash) < 22) {
  13. return false;
  14. }
  15. if (strlen($hash) > 22) {
  16. $hash = substr($hash,-22);
  17. }
  18. return base16_encode(base64_decode_ex($hash));
  19. }
  20.  
  21. /**
  22.  * Takes an input in base 256 and encodes it to base 16 using the Hex alphabet
  23.  * This function will not be commented. For more info:
  24.  * @see http://p...content-available-to-author-only...p.net/str-split
  25.  * @see http://p...content-available-to-author-only...p.net/sprintf
  26.  *
  27.  * @param string $str The value to convert
  28.  * @return string The base 16 rendering
  29.  */
  30. function base16_encode($str) {
  31. $byteArray = str_split($str);
  32. foreach ($byteArray as &$byte) {
  33. $byte = sprintf('%02x', ord($byte));
  34. }
  35. return join($byteArray);
  36. }
  37.  
  38. /**
  39.  * Decodes a base64 string based on the alphabet set in constant CRYPT_ALPHA
  40.  * Uses string functions rather than binary transformations, because said
  41.  * transformations aren't really much faster in PHP
  42.  * @params string $str The string to decode
  43.  * @return string The raw output, which may include unprintable characters
  44.  */
  45. function base64_decode_ex($str) {
  46. // set up the array to feed numerical data using characters as keys
  47. $alpha = array_flip(str_split(CRYPT_ALPHA));
  48. // split the input into single-character (6 bit) chunks
  49. $bitArray = str_split($str);
  50. $decodedStr = '';
  51. foreach ($bitArray as &$bits) {
  52. if ($bits == '$') { // $ indicates the end of the string, to stop processing here
  53. break;
  54. }
  55. if (!isset($alpha[$bits])) { // if we encounter a character not in the alphabet
  56. return false; // then break execution, the string is invalid
  57. }
  58. // decbin will only return significant digits, so use sprintf to pad to 6 bits
  59. $decodedStr .= sprintf('%06s', decbin($alpha[$bits]));
  60. }
  61. // there can be up to 6 unused bits at the end of a string, so discard them
  62. $decodedStr = substr($decodedStr, 0, strlen($decodedStr) - (strlen($decodedStr) % 8));
  63. $byteArray = str_split($decodedStr, 8);
  64. foreach ($byteArray as &$byte) {
  65. $byte = chr(bindec($byte));
  66. }
  67. return join($byteArray);
  68. }
  69.  
  70. echo md5_b64tob16($argv[1])."\n";
Success #stdin #stdout 0.02s 25716KB
stdin
vdfowU5bzsp7c6FfY9MK20 vdfowU5bzsp7c6FfY9MK20
stdout
define('CRYPT_ALPHA','./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz');
 
/*
 * Takes a 22 byte crypt-base-64 hash and converts it to base 16
 * If the input is longer than 22 chars (e.g., the entire output of crypt()),
 * then this function will strip all but the last 22.  Fails if under 22 chars
 *
 * @param string $hash  The hash to convert
 * @param string        The equivalent base16 hash (therefore a number)
 */
function md5_b64tob16($hash) {
    if (strlen($hash) < 22) {
        return false;
    }
    if (strlen($hash) > 22) {
        $hash = substr($hash,-22);
    }
    return base16_encode(base64_decode_ex($hash));
}
 
/**
 * Takes an input in base 256 and encodes it to base 16 using the Hex alphabet
 * This function will not be commented.  For more info:
 * @see http://p...content-available-to-author-only...p.net/str-split
 * @see http://p...content-available-to-author-only...p.net/sprintf
 *
 * @param string $str   The value to convert
 * @return string       The base 16 rendering
 */
function base16_encode($str) {
    $byteArray = str_split($str);
    foreach ($byteArray as &$byte) {
        $byte = sprintf('%02x', ord($byte));
    }
    return join($byteArray);
}
 
/**
 * Decodes a base64 string based on the alphabet set in constant CRYPT_ALPHA
 * Uses string functions rather than binary transformations, because said
 * transformations aren't really much faster in PHP
 * @params string $str  The string to decode
 * @return string       The raw output, which may include unprintable characters
 */
function base64_decode_ex($str) {
    // set up the array to feed numerical data using characters as keys
    $alpha = array_flip(str_split(CRYPT_ALPHA));
    // split the input into single-character (6 bit) chunks
    $bitArray = str_split($str);
    $decodedStr = '';
    foreach ($bitArray as &$bits) {
        if ($bits == '$') { // $ indicates the end of the string, to stop processing here
            break;
        }
        if (!isset($alpha[$bits])) { // if we encounter a character not in the alphabet
            return false;            // then break execution, the string is invalid
        }
        // decbin will only return significant digits, so use sprintf to pad to 6 bits
        $decodedStr .= sprintf('%06s', decbin($alpha[$bits]));
    }
    // there can be up to 6 unused bits at the end of a string, so discard them
    $decodedStr = substr($decodedStr, 0, strlen($decodedStr) - (strlen($decodedStr) % 8));
    $byteArray = str_split($decodedStr, 8);
    foreach ($byteArray as &$byte) {
        $byte = chr(bindec($byte));
    }
    return join($byteArray);
}
 
echo md5_b64tob16($argv[1])."\n";