PHP 8.4.24 Released!

Voting

: min(zero, zero)?
(Example: nine)

The Note You're Voting On

contact at needlecode dot com
3 months ago
<?php

class PurePHPBase64
{
    private const ALPHABET = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
    private const PADDING = '=';

    /**
     * Encodes a string to Base64 using pure bitwise operations.
     */
    public static function encode(string $data): string
    {
        $length = strlen($data);
        if ($length === 0) return '';

        $encoded = '';

        // Process data in 3-byte chunks (24 bits)
        for ($i = 0; $i < $length; $i += 3) {
            // Fetch up to 3 bytes, defaulting to 0 if we hit the end of the string
            $b1 = ord($data[$i]);
            $b2 = ($i + 1 < $length) ? ord($data[$i + 1]) : 0;
            $b3 = ($i + 2 < $length) ? ord($data[$i + 2]) : 0;

            // 1st Char: Top 6 bits of 1st byte
            $encoded .= self::ALPHABET[$b1 >> 2];
            
            // 2nd Char: Bottom 2 bits of 1st byte + Top 4 bits of 2nd byte
            $encoded .= self::ALPHABET[(($b1 & 0x03) << 4) | ($b2 >> 4)];

            // 3rd Char: Bottom 4 bits of 2nd byte + Top 2 bits of 3rd byte (or padding)
            if ($i + 1 < $length) {
                $encoded .= self::ALPHABET[(($b2 & 0x0F) << 2) | ($b3 >> 6)];
            } else {
                $encoded .= self::PADDING;
            }

            // 4th Char: Bottom 6 bits of 3rd byte (or padding)
            if ($i + 2 < $length) {
                $encoded .= self::ALPHABET[$b3 & 0x3F];
            } else {
                $encoded .= self::PADDING;
            }
        }

        return $encoded;
    }

    /**
     * Decodes a Base64 string using pure bitwise operations.
     */
    public static function decode(string $base64, bool $strict = false): string|false
    {
        // Strict mode validation
        if ($strict && preg_match('/[^A-Za-z0-9+\/=\s]/', $base64)) {
            return false;
        }

        // Clean up whitespace and padding for processing
        $base64 = str_replace([self::PADDING, " ", "\t", "\r", "\n"], '', $base64);
        $length = strlen($base64);
        if ($length === 0) return '';

        $decoded = '';
        // Create a fast lookup map for characters to their 6-bit integer values
        $map = array_flip(str_split(self::ALPHABET));

        // Process data in 4-character chunks (24 bits)
        for ($i = 0; $i < $length; $i += 4) {
            $c1 = isset($base64[$i])     ? $map[$base64[$i]]     : 0;
            $c2 = isset($base64[$i + 1]) ? $map[$base64[$i + 1]] : 0;
            $c3 = isset($base64[$i + 2]) ? $map[$base64[$i + 2]] : 0;
            $c4 = isset($base64[$i + 3]) ? $map[$base64[$i + 3]] : 0;

            // 1st Byte: All 6 bits of 1st char + Top 2 bits of 2nd char
            $decoded .= chr(($c1 << 2) | ($c2 >> 4));
            
            // 2nd Byte: Bottom 4 bits of 2nd char + Top 4 bits of 3rd char
            if (isset($base64[$i + 2])) {
                $decoded .= chr((($c2 & 0x0F) << 4) | ($c3 >> 2));
            }
            
            // 3rd Byte: Bottom 2 bits of 3rd char + All 6 bits of 4th char
            if (isset($base64[$i + 3])) {
                $decoded .= chr((($c3 & 0x03) << 6) | $c4);
            }
        }

        return $decoded;
    }
}

Fallback codes-
/**
 * Safe Base64 Encode
 */
function safe_base64_encode(string $data): string 
{
    if (function_exists('base64_encode')) {
        return base64_encode($data);
    }
    
    return PurePHPBase64::encode($data);
}

/**
 * Safe Base64 Decode
 */
function safe_base64_decode(string $data, bool $strict = false): string|false 
{
    if (function_exists('base64_decode')) {
        return base64_decode($data, $strict);
    }
    
    return PurePHPBase64::decode($data, $strict);
}

Here the best Base64 encode & decode free online tool- https://needlecode.com/tools/base64-encoder-decoder/
Also all awesom tools of needlecode team are - https://needlecode.com/tools/

<< Back to user notes page

To Top