Skip to content

Commit

Permalink
use bytes as input/output just as the core base64
Browse files Browse the repository at this point in the history
  • Loading branch information
jschlyter committed Apr 3, 2021
1 parent a4a02ee commit be3a68d
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 16 deletions.
15 changes: 10 additions & 5 deletions base45/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@
Base45 Data Encoding as described in draft-faltstrom-base45-02
"""

from typing import Union

BASE45_CHARSET = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ $%*+-./:"


def b45encode(buf: bytes) -> str:
def b45encode(buf: bytes) -> bytes:
"""Convert bytes to base45-encoded string"""
res = ""
buflen = len(buf)
Expand All @@ -19,14 +21,17 @@ def b45encode(buf: bytes) -> str:
x = buf[i]
d, c = divmod(x, 45)
res += BASE45_CHARSET[c] + BASE45_CHARSET[d]
return res
return res.encode()


def b45decode(s: str) -> bytes:
def b45decode(s: Union[bytes, str]) -> bytes:
"""Decode base45-encoded string to bytes"""
res = []
try:
buf = [BASE45_CHARSET.index(c) for c in s]
if isinstance(s, str):
buf = [BASE45_CHARSET.index(c) for c in s]
else:
buf = [BASE45_CHARSET.index(c) for c in s.decode()]
buflen = len(buf)
for i in range(0, buflen, 3):
x = buf[i] + buf[i + 1] * 45
Expand All @@ -36,5 +41,5 @@ def b45decode(s: str) -> bytes:
else:
res.append(x)
return bytes(res)
except (ValueError, IndexError):
except (ValueError, IndexError, AttributeError):
raise ValueError("Invalid base45 string")
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "base45"
version = "0.1.1"
version = "0.2.0"
description = "Base45 Encoder/Decoder"
authors = ["Jakob Schlyter <[email protected]>"]
license = "BSD-2-Clause"
Expand Down
20 changes: 10 additions & 10 deletions test/test_base45.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,21 @@
from base45 import b45decode, b45encode

GOOD_DATA = [
(b"", ""),
(b"AB", "BB8"),
(b"Hello!!", "%69 VD92EX0"),
(b"base-45", "UJCLQE7W581"),
(b"ietf!", "QED8WEX0"),
(b"\x0d\x0e\x0a\x0d\x0c\x00\x0f\x0f\x0e!", "CT18C1CN1U+1HZ1"),
(b"\x00\x00\x00\x00\x00\x00\x00\x00", "000000000000"),
(b"\x00\x00\x00\x00\x00\x00\x00", "00000000000"),
(b"", b""),
(b"AB", b"BB8"),
(b"Hello!!", b"%69 VD92EX0"),
(b"base-45", b"UJCLQE7W581"),
(b"ietf!", b"QED8WEX0"),
(b"\x0d\x0e\x0a\x0d\x0c\x00\x0f\x0f\x0e!", b"CT18C1CN1U+1HZ1"),
(b"\x00\x00\x00\x00\x00\x00\x00\x00", b"000000000000"),
(b"\x00\x00\x00\x00\x00\x00\x00", b"00000000000"),
(
b"The quick brown fox jumps over the lazy dog",
"8UADZCKFEOEDJOD2KC54EM-DX.CH8FSKDQ$D.OE44E5$CS44+8DK44OEC3EFGVCD2",
b"8UADZCKFEOEDJOD2KC54EM-DX.CH8FSKDQ$D.OE44E5$CS44+8DK44OEC3EFGVCD2",
),
]

BAD_BASE45_STRINGS = ["xyzzy", "::::", "a"]
BAD_BASE45_STRINGS = [b"xyzzy", b"::::", b"a", 42]


class TestBase45(unittest.TestCase):
Expand Down

0 comments on commit be3a68d

Please sign in to comment.