From e71f3bbc36eba8abf616d93fde48cba66b3749f1 Mon Sep 17 00:00:00 2001 From: jesopo Date: Fri, 6 Dec 2019 12:10:41 +0000 Subject: [PATCH] add utils.parse.shortencode() and utils.parse.shortdecode - effectively base62 --- src/utils/parse.py | 46 +++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 45 insertions(+), 1 deletion(-) diff --git a/src/utils/parse.py b/src/utils/parse.py index ce2ee793..f6d43bff 100644 --- a/src/utils/parse.py +++ b/src/utils/parse.py @@ -1,4 +1,4 @@ -import decimal, io, typing +import decimal, io, random, typing from . import datetime, errors COMMENT_TYPES = ["#", "//"] @@ -120,3 +120,47 @@ def timed_args(args, min_args): return time, args[1:] return None, args +SHORTENCODE_CHARS = list( + "abcdefghijklmnopqrstuvwxyz" + "ABCDEFGHIJKLMNOPQRSTUVWXYZ" + "0123456789") + +SHORTENCODE_SALT = "bitbot" +def _shortencode_salt(salt: str): + chars = SHORTENCODE_CHARS.copy() + random.Random(salt).shuffle(chars) + return chars + +def _shortencode_tobase(n: int, base: int) -> typing.List[int]: + r = [] + while n: + n, remainder = divmod(n, base) + r.append(remainder) + return r +def _shortencode_frombase(ints: typing.List[int], base: int) -> int: + n = 0 + for i in ints: + n = (n*base)+i + return n + +def shortencode_b(bytes: bytes, salt: str=SHORTENCODE_SALT): + chars = _shortencode_salt(salt) + + ints = list(bytes) + n = _shortencode_frombase(ints, 256) + r = _shortencode_tobase(n, len(chars)) + + return "".join(chars[i] for i in reversed(r)) +def shortencode(s: str, salt: str=SHORTENCODE_SALT): + return shortencode_b(s.encode("latin-1"), salt) + +def shortdecode_b(s: str, salt: str=SHORTENCODE_SALT): + chars = _shortencode_salt(salt) + + ints = [chars.index(c) for c in s] + n = _shortencode_frombase(ints, len(chars)) + r = _shortencode_tobase(n, 256) + + return bytes(reversed(r)) +def shortdecode(s: str, salt: str=SHORTENCODE_SALT): + return shortdecode_b(s, salt).decode("latin-1")