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)
{
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)
source
share