Skip to content

Commit

Permalink
πŸ—“ Sep 25, 2023 7:52:57 PM
Browse files Browse the repository at this point in the history
✨ to/from_base62
πŸ§ͺ tests added/updated
πŸ™ plugins updated
  • Loading branch information
securisec committed Sep 25, 2023
1 parent 1968a96 commit bcf3a62
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 2 deletions.
1 change: 1 addition & 0 deletions TODO
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ New ideas:
☐ πŸ™ diff show results only
☐ ✨ brainfuck encoder/decoder
☐ ✨ spoon encoder/decoder
base62

Bug:

Expand Down
2 changes: 1 addition & 1 deletion chepy/__version__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
__version__ = "6.2.0" # pragma: no cover
__version__ = "6.3.0" # pragma: no cover
__author__ = "@securisec" # pragma: no cover
2 changes: 1 addition & 1 deletion chepy/chepy_plugins
45 changes: 45 additions & 0 deletions chepy/modules/dataformat.py
Original file line number Diff line number Diff line change
Expand Up @@ -1921,3 +1921,48 @@ def rotate_left(self, radix: int = 1, carry: bool = False) -> DataFormatT:
else:
self.state = r.rot(Rotate.rotate_left)
return self

@ChepyDecorators.call_stack
def to_base62(
self,
alphabet: str = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
) -> DataFormatT:
"""Encode to base62
Args:
alphabet (str, optional): Alphabet. Defaults to "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz".
Returns:
Chepy: The Chepy object
"""
base62 = []
num = int.from_bytes(self._convert_to_bytes(), byteorder="big")

while num > 0:
num, remainder = divmod(num, 62)
base62.insert(0, alphabet[remainder])

self.state = "".join(base62)
return self

@ChepyDecorators.call_stack
def from_base62(
self,
alphabet: str = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
) -> DataFormatT:
"""Decode from base62
Args:
alphabet (str, optional): Alphabet. Defaults to "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz".
Returns:
Chepy: The Chepy object.
"""
base62_dict = {char: index for index, char in enumerate(alphabet)}
num = 0
for char in self._convert_to_str():
num = num * 62 + base62_dict[char]
decoded_data = num.to_bytes((num.bit_length() + 7) // 8, byteorder="big")

self.state = decoded_data
return self
2 changes: 2 additions & 0 deletions chepy/modules/dataformat.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -103,3 +103,5 @@ class DataFormat(ChepyCore):
def from_base(self: DataFormatT, radix: int=16) -> DataFormatT: ...
def rotate_right(self: DataFormatT, radix: int=1, carry: bool=False) -> DataFormatT: ...
def rotate_left(self: DataFormatT, radix: int=1, carry: bool=False) -> DataFormatT: ...
def to_base62(self: DataFormatT, alphabet: str="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz") -> DataFormatT: ...
def from_base62(self: DataFormatT, alphabet: str="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz") -> DataFormatT: ...
13 changes: 13 additions & 0 deletions tests/test_ctf.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,16 @@ def test_springforward_23_hours_behind():
c = Chepy("vqkk{0vtg_b1um_e1tt_b3tt}")
c.rotate_bruteforce().dict_get_items().filter_list("nicc")
assert c.o == b"nicc{0nly_t1me_w1ll_t3ll}"


def test_bsides_c5():
c = (
Chepy(
"BtC8EzBDHPOhKvzY6zyWRuy4lFMQNxDk9zLIG93n50uD83gxB9vg5jq7ZQJ50xpHMrUCwk4dRwtA0yGRSIDTFZ"
)
.from_base62()
.reverse()
.from_octal()
.o
== b"th3_cak3_is_a_li3"
)
8 changes: 8 additions & 0 deletions tests/test_dataformat.py
Original file line number Diff line number Diff line change
Expand Up @@ -622,3 +622,11 @@ def test_rotate_left():
Chepy("d445d4a7e477d3d3").from_hex().rotate_left(radix=4, carry=True).o
== b"D]J~G}=="
)


def test_base62():
assert (
Chepy(b"hello !!123!! world\xcc").to_base62().o
== b"EtWxGEdLPmwchEzFHWNyyyI3p1w"
)
assert Chepy("3bfP0XZgTym6SsUKeZS5Z6qoKa").from_base62().o == b"hello !!123!! world"

0 comments on commit bcf3a62

Please sign in to comment.