Phpass custom base 64 encoder: does it have a name / advantage over Base64?

phpass uses a strange (for me) algorithm in encode64 () up to the 64th code. Base64 and Uuencode linearly piecewise 6 bits to create each octet before mapping to a print char. encode64moves a bit around:

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

Is this algorithm well known? And besides backward compatibility, why choose it with Base64 ?

I rewrote it below to refine 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 moves)

+5
source share
2

encode64() base64, - , 6 , . , ; ( PHP base64_encode).

+1

encode64 '.' '/' a-zA-Z0-9. Base64 "+" "/". '.' '/' 0 1, '+' '/' 62 63 Base64.

UUencode , .

encode64. , , , "+". Base64 str_replace.

0

All Articles