public static function encode($bytes)
    {
        $i = 0; $index = 0;
        $base32 = '';
        $bytesLen = \strlen($bytes);

        while ($i < $bytesLen) {
            $currByte = \ord($bytes[$i]);

            /* Is the current digit going to span a byte boundary? */
            if ($index > 3) {
                if (($i + 1) < $bytesLen) {
                    $nextByte = \ord($bytes[$i+1]);
                } else {
                    $nextByte = 0;
                }

                $digit = $currByte & (0xFF >> $index);
                $index = ($index + 5) % 8;
                $digit <<= $index;
                $digit |= $nextByte >> (8 - $index);
                $i++;
            } else {
                $digit = ($currByte >> (8 - ($index + 5))) & 0x1F;
                $index = ($index + 5) % 8;
                if ($index === 0) {
                    $i++;
                }
            }

            $base32 .= self::$base32Chars[$digit];
        }
        return $base32;
    }