Skip to content

Commit

Permalink
🗓 Jul 18, 2023 6:06:16 PM
Browse files Browse the repository at this point in the history
🔧 fix #27
🚀 update to/from binary completely with byte length
🔥 rename hex_to_binary to hex_to_bytes
🤖 types added/updated
🧪 tests added/updated
  • Loading branch information
securisec committed Jul 18, 2023
1 parent 3595273 commit 5ac00b5
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 23 deletions.
10 changes: 9 additions & 1 deletion TODO
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,15 @@ New ideas:
☐ 💡 maybe a decorator function to convert all inputs into bytes when possible? this will allow for a consistant bytes approach to all functions
☐ rsa enc/dec with key or from pem directly
☐ ✨ jwt hmac confusion
☐ 🚀 improve tar compression mode type
☐ ✨ amf encode/decode
☐ ✨ rabbit encryption
☐ ✨ lz4 compression/decompression
☐ ✨ aes cmac
☐ ✨ shuffle
☐ ✨ cetacean encode/decode
☐ ✨ ls47 enc/dec
☐ ✨ lm hash
☐ ✨ nt hash

Bug:

Expand Down
1 change: 0 additions & 1 deletion chepy/core.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ class ChepyCore:
@property
def out(self: ChepyCoreT) -> ChepyCoreT: ...
def out_as_str(self: ChepyCoreT) -> str: ...
def out_as_bytes(self: ChepyCoreT) -> bytes: ...
def get_by_index(self: ChepyCoreT, index: int) -> ChepyCoreT: ...
def get_by_key(self: ChepyCoreT, key: str) -> ChepyCoreT: ...
def copy_to_clipboard(self: ChepyCoreT) -> None: ...
Expand Down
2 changes: 1 addition & 1 deletion chepy/modules/compression.py
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ def zlib_decompress(self) -> CompressionT:
Examples:
>>> c = Chepy("789c0580a10d000008c35ee1b9ca05c104e737b761ca5711e8039a")
>>> c.hex_to_binary()
>>> c.hex_to_bytes()
>>> c.zlib_decompress()
>>> c.out
b"some text"
Expand Down
38 changes: 25 additions & 13 deletions chepy/modules/dataformat.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import regex as re
import hexdump
from ast import literal_eval
from typing import TypeVar, Union
from typing import TypeVar, Union, List
from urllib.parse import quote_plus as _urllib_quote_plus
from urllib.parse import unquote_plus as _urllib_unquote_plus

Expand Down Expand Up @@ -604,17 +604,17 @@ def hex_to_int(self) -> DataFormatT:
return self

@ChepyDecorators.call_stack
def hex_to_binary(self) -> DataFormatT:
"""Hex to binary hex
def hex_to_bytes(self) -> DataFormatT:
"""Hex to bytes hex
Converts a hex string to its binary form. Example:
Converts a hex string to its bytes form. Example:
41 becomes \\x41
Returns:
Chepy: The Chepy object.
Examples:
>>> Chepy("ab00").hex_to_binary().o
>>> Chepy("ab00").hex_to_bytes().o
b"\\xab\\x00"
"""
self.state = binascii.unhexlify(self._convert_to_bytes())
Expand Down Expand Up @@ -931,39 +931,51 @@ def from_decimal(self, delimiter: str = " ", join_by: str = "") -> DataFormatT:
return self

@ChepyDecorators.call_stack
def to_binary(self, join_by: str = " ") -> DataFormatT:
def to_binary(
self, join_by: Union[str, bytes] = " ", byte_length: int = 8
) -> DataFormatT:
"""Convert string characters to binary
Args:
join_by (str, optional): join_by. Defaults to " ".
byte_length (int, optional): Byte length. Defaults to 8.
Returns:
Chepy: The Chepy object.
Examples:
>>> Chepy("abc").to_binary().o
"01100001 01100010 01100011"
b"01100001 01100010 01100011"
"""
self.state = join_by.join(
list(format(ord(s), "08b") for s in list(self._convert_to_str()))
)
hold = []
join_by = self._str_to_bytes(join_by)
out = list(format(s, "08b").encode() for s in list(self._convert_to_bytes()))
for s in list(self._convert_to_bytes()):
hold.append(str(bin(s)[2:].zfill(byte_length)).encode())
self.state = join_by.join(hold)
return self

@ChepyDecorators.call_stack
def from_binary(self, delimiter: str = " ") -> DataFormatT:
def from_binary(self, delimiter: str = " ", byte_length: int = 8) -> DataFormatT:
"""Convert a list of binary numbers to string
Args:
delimiter (str, optional): Delimiter. Defaults to " ".
byte_length (int, optional): Byte length. Defaults to 8.
Returns:
Chepy: The Chepy object.
Examples:
>>> Chepy(["01100001", "01100010", "01100011"]).from_binary().o
>>> Chepy("01100001 01100010 01100011").from_binary().o
"abc"
"""
n = int("".join(self._convert_to_str().split(delimiter)), 2)
n = int(
"".join(
[x[byte_length - 8 :] for x in self._convert_to_str().split(delimiter)]
),
2,
)
self.state = n.to_bytes((n.bit_length() + 7) // 8, "big")
return self

Expand Down
6 changes: 3 additions & 3 deletions chepy/modules/dataformat.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class DataFormat(ChepyCore):
def to_hex(self: DataFormatT, delimiter: str=..., join_by: str=...) -> DataFormatT: ...
def from_hex(self: DataFormatT, delimiter: str=...) -> DataFormatT: ...
def hex_to_int(self: DataFormatT) -> DataFormatT: ...
def hex_to_binary(self: DataFormatT) -> DataFormatT: ...
def hex_to_bytes(self: DataFormatT) -> DataFormatT: ...
def hex_to_str(self: DataFormatT, ignore: bool=...) -> DataFormatT: ...
def str_to_hex(self: DataFormatT) -> DataFormatT: ...
def int_to_hex(self: DataFormatT) -> DataFormatT: ...
Expand All @@ -54,8 +54,8 @@ class DataFormat(ChepyCore):
def from_charcode(self: DataFormatT, delimiter: str=..., join_by: str=..., base: int=...) -> DataFormatT: ...
def to_decimal(self: DataFormatT) -> DataFormatT: ...
def from_decimal(self: DataFormatT, delimiter: str=..., join_by: str=...) -> DataFormatT: ...
def to_binary(self: DataFormatT, join_by: str=...) -> DataFormatT: ...
def from_binary(self: DataFormatT, delimiter: str=...) -> DataFormatT: ...
def to_binary(self: DataFormatT, join_by: Union[bytes, str]=..., byte_length: int=...) -> DataFormatT: ...
def from_binary(self: DataFormatT, delimiter: Union[bytes, str]=..., byte_length: int=...) -> DataFormatT: ...
def to_octal(self: DataFormatT, join_by: str=...) -> DataFormatT: ...
def from_octal(self: DataFormatT, delimiter: str=..., join_by: str=...) -> DataFormatT: ...
def to_html_entity(self: DataFormatT) -> DataFormatT: ...
Expand Down
2 changes: 1 addition & 1 deletion tests/test_compression.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ def test_zlib_compress():
def test_zlib_decompress():
assert (
Chepy("789c0580a10d000008c35ee1b9ca05c104e737b761ca5711e8039a")
.hex_to_binary()
.hex_to_bytes()
.zlib_decompress()
.o
== b"some text"
Expand Down
28 changes: 25 additions & 3 deletions tests/test_dataformat.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,8 +169,8 @@ def test_hex_to_int():
assert Chepy("123").hex_to_int().out == 291


def test_hex_to_binary():
assert Chepy("ab00").hex_to_binary().o == b"\xab\x00"
def test_hex_to_bytes():
assert Chepy("ab00").hex_to_bytes().o == b"\xab\x00"


def test_int_to_hex():
Expand Down Expand Up @@ -279,11 +279,33 @@ def test_from_decimal():


def test_to_binary():
assert Chepy("abc").to_binary().o == "01100001 01100010 01100011"
assert Chepy("abc").to_binary().o == b"01100001 01100010 01100011"
assert Chepy("8081").from_hex().to_binary().o == b"10000000 10000001"


def test_from_binary():
assert Chepy("011001000110 000101110100 0110000 1").from_binary().o == b"data"
assert Chepy("01100100 01100001 01110100 01100001").from_binary().o == b"data"
assert (
Chepy("0001100100 0001100001 0001110100 0001100001")
.from_binary(byte_length=10)
.o
== b"data"
)
assert (
Chepy("000001100100 000001100001 000001110100 000001100001")
.from_binary(byte_length=12)
.o
== b"data"
)
assert (
Chepy(
"00000000000001110011 00000000000001101111 00000000000001101101 00000000000001100101 00000000000000100000 00000000000001100100 00000000000001100001 00000000000001110100 00000000000001100001"
)
.from_binary(byte_length=20)
.o
== b"some data"
)


def test_to_octal():
Expand Down

0 comments on commit 5ac00b5

Please sign in to comment.