GithubHelp home page GithubHelp logo
  • šŸ‘‹ Hi, Iā€™m @genigre
  • šŸ‘€ Iā€™m interested in ...
  • šŸŒ± Iā€™m currently learning ...
  • šŸ’žļø Iā€™m looking to collaborate on ...
  • šŸ“« How to reach me ...

""" Base45 Data Encoding as described in draft-faltstrom-base45-02 """

from typing import Union

BASE45_CHARSET = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ $%*+-./:" BASE45_DICT = {v: i for i, v in enumerate(BASE45_CHARSET)}

def b45encode(buf: bytes) -> bytes: """Convert bytes to base45-encoded string""" res = "" buflen = len(buf) for i in range(0, buflen & ~1, 2): x = (buf[i] << 8) + buf[i + 1] e, x = divmod(x, 45 * 45) d, c = divmod(x, 45) res += BASE45_CHARSET[c] + BASE45_CHARSET[d] + BASE45_CHARSET[e] if buflen & 1: d, c = divmod(buf[-1], 45) res += BASE45_CHARSET[c] + BASE45_CHARSET[d] return res.encode()

def b45decode(s: Union[bytes, str]) -> bytes: """Decode base45-encoded string to bytes""" try: if isinstance(s, str): buf = [BASE45_DICT[c] for c in s.rstrip("\n")] elif isinstance(s, bytes): buf = [BASE45_DICT[c] for c in s.decode()] else: raise TypeError("Type must be 'str' or 'bytes'")

    buflen = len(buf)
    if buflen % 3 == 1:
        raise ValueError("Invalid base45 string")

    res = []
    for i in range(0, buflen, 3):
        if buflen - i >= 3:
            x = buf[i] + buf[i + 1] * 45 + buf[i + 2] * 45 * 45
            if x > 0xFFFF:
                raise ValueError
            res.extend(divmod(x, 256))
        else:
            x = buf[i] + buf[i + 1] * 45
            if x > 0xFF:
                raise ValueError
            res.append(x)
    return bytes(res)
except (ValueError, KeyError, AttributeError):
    raise ValueError("Invalid base45 string")

genigre's Projects

genigre icon genigre

Config files for my GitHub profile.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    šŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. šŸ“ŠšŸ“ˆšŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ā¤ļø Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.