简体   繁体   中英

phpass's custom base 64 encoder: does it have a name/advantage over Base64?

phpass uses a strange (to me) algorithm in encode64() to base 64 encode. Base64 and Uuencode linearly chunk 6 bits to produce each octet before mapping to a printable char. encode64 shuffles the bits around:

input bit location:    abcdefgh ijklmnop qrstuvwx
base64 bit location:   ..abcdef ..ghijkl ..mnopqr ..stuvwx
encode64 bit location: ..cdefgh ..mnopab ..wxijkl ..qrstuv

Is this algorithm commonly known? And besides backward compatibility, why choose it over Base64 ?

Below I've rewritten it to clarify the algorithm:

function encode64($input, $bytesToProcess)
{
    // convert to array of ints
    for ($i = 0; $i < $bytesToProcess; $i++) {
        $bytes[] = ord($input[$i]);
    }

    $octets = array();
    $i = 0;
    do {
        $value = $bytes[$i++];
        $octets[] = $value & 0x3f;
        if ($i < $bytesToProcess) {
            $value |= $bytes[$i] << 8;
        }
        $octets[] = ($value >> 6) & 0x3f;
        if ($i++ >= $bytesToProcess) {
            break;
        }
        if ($i < $bytesToProcess) {
            $value |= $bytes[$i] << 16;
        }
        $octets[] = ($value >> 12) & 0x3f;
        if ($i++ >= $bytesToProcess) {
            break;
        }
        $octets[] = ($value >> 18) & 0x3f;
    } while ($i < $bytesToProcess);

    return array_map(function ($i) {
        return str_pad(base_convert($i, 10, 2), 6, '0', STR_PAD_LEFT);
    }, $octets);
}

var_export(encode64("Man", 3));

(updated to indicate exactly where each input bit is moved)

encode64() just looks like an implementation of standard base64 which counts bits in the reverse order and uses a different character set -- if you squint your eyes the right way, it's selecting the last 6 bits of the first byte for the first output character, for instance. This is probably just a mistake; there's no security or performance benefit in doing it this way (and some performance drawbacks relative to PHP's native base64_encode ).

encode64 uses '.' and '/' beside a-zA-Z0-9. Base64 uses '+' and '/'. Also '.'and '/' map to 0 and 1, while '+' and '/' map to 62 and 63 in Base64.

UUencode uses letters, digits, and many punctuation characters to be compatible with systems without capitals.

I'm not familiar with encode64. The only reason for using it would be if '+' is not allowed in the environment that you use. But then you could just use Base64 and do a str_replace.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM