pybase64

repository·master·Indexed 41 days ago

https://github.com/mayeut/pybase64

A high-performance Python wrapper around libbase64 providing fast Base64 encoding and decoding. It serves as a drop-in replacement for the standard Python base64 module's modern interface, featuring specialized helpers for standard and URL-safe operations, a CLI tool for encoding, decoding, and benchmarking, and support for SIMD instruction sets like AVX2.

Tokens
2.7K
Snippets
14
Records
20
Agent score
46%

What's inside pybase64

  1. Use pybase64 with the standard base64 API

    master

    The pybase64 library uses the same API as Python's standard base64 module (the modern interface introduced in Python 2.4), making it a drop-in replacement for easy integration.

    For maximum performance during decoding, it is recommended to use pybase64.b64decode with validate=True whenever possible.

    import pybase64
    
    # Standard encoding/decoding with altchars
    print(pybase64.b64encode(b'>>>foo???', altchars='_:'))
    # b'Pj4_Zm9vPz8:'
    print(pybase64.b64decode(b'Pj4_Zm9vPz8:', altchars='_:', validate=True))
    # b'>>>foo???'
  2. Use pybase64 API

    master

    The pybase64 module uses the same API as the Python base64 "modern interface" (introduced in Python 2.4), making it a drop-in replacement for easy integration.

    To achieve the highest decoding performance, it is recommended to use pybase64.b64decode with the following parameters when possible:

    • validate=True (or ignorechars=b'')
    • padded=True
    import pybase64
    
    # Basic encoding/decoding with altchars
    print(pybase64.b64encode(b'>>>foo???', altchars='_:'))
    # b'Pj4_Zm9vPz8:'
    print(pybase64.b64decode(b'Pj4_Zm9vPz8:', altchars='_:', validate=True))
    # b'>>>foo???'
    
    # Standard encoding helpers
    print(pybase64.standard_b64encode(b'>>>foo???'))
    # b'Pj4+Zm9vPz8/'
    print(pybase64.standard_b64decode(b'Pj4+Zm9vPz8/'))
    # b'>>>foo???'
    
    # URL safe encoding helpers
    print(pybase64.urlsafe_b64encode(b'>>>foo???'))
    # b'Pj4-Zm9vPz8_'
    print(pybase64.urlsafe_b64decode(b'Pj4-Zm9vPz8_'))
    # b'>>>foo???'
  3. Get pybase64 version and license information

    master

    You can programmatically retrieve metadata about the library using:

    • get_version(): Returns the current version of pybase64.
    • get_license_text(): Returns the license text associated with the project.
  4. Use the Main Base64 API

    master

    The core API provides functions for standard Base64 encoding and decoding. Use b64encode and b64decode for standard operations, or their specialized variants for specific return types:

    • b64encode(data): Encodes bytes to Base64 bytes.
    • b64encode_as_string(data): Encodes bytes to a Base64 string.
    • b64decode(data): Decodes Base64 bytes/string back to bytes.
    • b64decode_as_bytearray(data): Decodes Base64 data into a bytearray object.
  5. Use Base64 encoding helpers

    master

    The Helpers API provides explicit methods for standard and URL-safe Base64 encoding/decoding:

    • standard_b64encode(data): Performs standard Base64 encoding.
    • standard_b64decode(data): Performs standard Base64 decoding.
    • urlsafe_b64encode(data): Performs URL-safe Base64 encoding (using - and _ instead of + and /).
    • urlsafe_b64decode(data): Performs URL-safe Base64 decoding.
  6. Standard and URL-safe encoding helpers

    master

    In addition to the standard b64encode/b64decode methods, pybase64 provides explicit helpers for standard and URL-safe base64 operations.

    Standard Helpers

    • standard_b64encode(s)
    • standard_b64decode(s)

    URL-safe Helpers

    • urlsafe_b64encode(s)
    • urlsafe_b64decode(s)
  7. Use standard encoding helpers

    master

    Use standard_b64encode and standard_b64decode for standard base64 operations without needing to manually specify altchars.

    import pybase64
    
    print(pybase64.standard_b64encode(b'>>>foo???'))
    # b'Pj4+Zm9vPz8/'
    print(pybase64.standard_b64decode(b'Pj4+Zm9vPz8/'))
    # b'>>>foo???'
  8. Use URL safe encoding helpers

    master

    Use urlsafe_b64encode and urlsafe_b64decode for base64 encoding that is safe for use in URLs.

    import pybase64
    
    print(pybase64.urlsafe_b64encode(b'>>>foo???'))
    # b'Pj4-Zm9vPz8_'
    print(pybase64.urlsafe_b64decode(b'Pj4-Zm9vPz8_'))
    # b'>>>foo???'
  9. Use the pybase64 CLI tool

    master

    The pybase64 package provides a command-line interface with subcommands for encoding, decoding, and benchmarking.

    # General usage pattern
    pybase64 {benchmark,encode,decode} [options]
    
    # Example: view help
    pybase64 --help
    
    # Example: view version
    pybase64 -V