Skip to content

Commit

Permalink
🗓 Jun 27, 2023 9:29:51 PM
Browse files Browse the repository at this point in the history
🧪 tests added/updated
✨ long_bytes and bytes_to_long
  • Loading branch information
securisec committed Jun 28, 2023
1 parent b5d01a6 commit 56d7154
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 1 deletion.
26 changes: 25 additions & 1 deletion chepy/modules/dataformat.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
from urllib.parse import quote_plus as _urllib_quote_plus
from urllib.parse import unquote_plus as _urllib_unquote_plus

crypto_number = lazy_import.lazy_module("Crypto.Util.number")

from ..core import ChepyCore, ChepyDecorators
from chepy.modules.internal.constants import Encoding

Expand Down Expand Up @@ -1355,7 +1357,7 @@ def swap_endianness(self) -> DataFormatT:
return self

@ChepyDecorators.call_stack
def bruteforce_from_base_xx(self):
def bruteforce_from_base_xx(self) -> DataFormatT:
"""Bruteforce various base encodings. Current supports base85, base16, base32, base64, base85, base58
Returns:
Expand All @@ -1377,4 +1379,26 @@ def bruteforce_from_base_xx(self):
except:
hold[do[0]] = None
self.state = hold
return self

@ChepyDecorators.call_stack
def long_to_bytes(self) -> DataFormatT:
"""Long numbers to bytes
Returns:
Chepy: The Chepy object.
"""
d = self._convert_to_int()
self.state = crypto_number.long_to_bytes(d)
return self

@ChepyDecorators.call_stack
def bytes_to_long(self) -> DataFormatT:
"""Bytes to long
Returns:
Chepy: The Chepy object.
"""
d = self._convert_to_bytes()
self.state = crypto_number.bytes_to_long(d)
return self
2 changes: 2 additions & 0 deletions chepy/modules/dataformat.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,5 @@ class DataFormat(ChepyCore):
def from_base91(self: DataFormatT) -> DataFormatT: ...
def swap_endianness(self: DataFormatT) -> DataFormatT: ...
def bruteforce_from_base_xx(self: DataFormatT) -> DataFormatT: ...
def long_to_bytes(self: DataFormatT) -> DataFormatT: ...
def bytes_to_long(self: DataFormatT) -> DataFormatT: ...
18 changes: 18 additions & 0 deletions tests/test_dataformat.py
Original file line number Diff line number Diff line change
Expand Up @@ -426,3 +426,21 @@ def test_swap_endianness():

def test_bruteforce_base_xx():
assert Chepy("dGVzdA==").bruteforce_from_base_xx().o["base64"] == b"test"


def test_long_to_bytes():
assert (
Chepy(
"11515195063862318899931685488813747395775516287289682636499965282714637259206269"
)
.long_to_bytes()
.o
== b"crypto{3nc0d1n6_4ll_7h3_w4y_d0wn}"
)


def test_bytes_to_long():
assert (
Chepy("crypto{3nc0d1n6_4ll_7h3_w4y_d0wn}").bytes_to_long().o
== 11515195063862318899931685488813747395775516287289682636499965282714637259206269
)

0 comments on commit 56d7154

Please sign in to comment.