Skip to content

Commit

Permalink
πŸ“… Commit Sun, 26 Sep 2021 11:51:30
Browse files Browse the repository at this point in the history
✨ for_each method to iterate over a list state and apply methods
✨ to_string method
✨ select method to extract a range
✨ length method to get length of state
πŸ› ignore hosname validation in get_ssl_cert
  • Loading branch information
securisec committed Sep 26, 2021
1 parent 2661bf2 commit 264a542
Show file tree
Hide file tree
Showing 8 changed files with 111 additions and 1 deletion.
1 change: 1 addition & 0 deletions TODO
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ Misc:
☐ cyberchef recipe to chepy recipe converter

Archive:
βœ” fork for each @project(New ideas)
βœ” convert int to specified base. like int(.., base) @project(New ideas)
βœ” crib dragging @project(New ideas)
βœ” xor data with key @project(Utility)
Expand Down
49 changes: 48 additions & 1 deletion chepy/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def call_stack(func, *args, **kwargs):


class ChepyCore(object):
"""The `ChepyCore` class for Chepy is primarily used as an interface
"""The ChepyCore class for Chepy is primarily used as an interface
for all the current modules/classes in Chepy, or for plugin development.
The `ChepyCore` class is what provides the various attributes like **states**,
**buffers**, etc and is required to use and extend Chepy.
Expand Down Expand Up @@ -231,6 +231,53 @@ def fork(self, methods: List[Tuple[Union[str, object], dict]]):
self.states[i] = getattr(self, method_name)().o
return self

def for_each(self, methods: List[Tuple[Union[str, object], dict]]):
"""Run multiple methods on current state if it is a list
Method names in a list of tuples. If using in the cli,
this should not contain any spaces.
Args:
methods (List[Tuple[Union[str, object], dict]]): Required.
List of tuples
Returns:
Chepy: The Chepy object.
Examples:
This method takes an array of method names and their args as an list of
tuples; the first value of the tuple is the method name as either a string,
or as an object, and the second value is a ditionary of arguments. The keys of
in the dictionary are method argument names, while the values are argument
values.
>>> from chepy import Chepy
>>> c = Chepy(['41', '42'])
>>> c.for_each([("from_hex",), ("to_hex",)])
>>> # this is how to use fork methods with a string
>>> c.for([(c.from_hex,), (c.to_hex,)])
>>> # This is how to use fork using methods
>>> print(c)
['41', '42']
"""
assert isinstance(self.state, list), "Current state is not a list"
hold = self.state
for i, val in enumerate(hold):
self.state = val
for method in methods:
if type(method[0]).__name__ == "method":
method_name = method[0].__name__ # pragma: no cover
elif isinstance(method[0], str):
method_name = method[0]
if len(method) > 1:
hold[i] = getattr(self, method_name)(
**method[1]
).o # pragma: no cover
else:
hold[i] = getattr(self, method_name)().o
self.state = hold
return self

@ChepyDecorators.call_stack
def set_state(self, data: Any):
"""Set any arbitrary values in the current state
Expand Down
1 change: 1 addition & 0 deletions chepy/core.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class ChepyCore:
@state.setter
def state(self: ChepyCoreT, val: Any) -> None: ...
def fork(self: ChepyCoreT, methods: List[Tuple[Union[str, object], dict]]) -> ChepyCoreT: ...
def for_each(self: ChepyCoreT, methods: List[Tuple[Union[str, object], dict]]) -> ChepyCoreT: ...
def set_state(self: ChepyCoreT, data: Any) -> ChepyCoreT: ...
def create_state(self: ChepyCoreT): ...
def copy_state(self: ChepyCoreT, index: int=...) -> ChepyCoreT: ...
Expand Down
37 changes: 37 additions & 0 deletions chepy/modules/dataformat.py
Original file line number Diff line number Diff line change
Expand Up @@ -1083,3 +1083,40 @@ def swap_strings(self, by: int) -> DataFormatT:
t[::by], t[1::by] = t[1::by], t[::by]
self.state = "".join(t)
return self

@ChepyDecorators.call_stack
def to_string(self) -> DataFormatT:
"""Convert to string
Returns:
Chepy: The Chepy object
"""
self.state = self._convert_to_str()
return self

@ChepyDecorators.call_stack
def select(self, start: int, end: int = None) -> DataFormatT:
"""Get an item by specifying an index
Args:
start (int): Starting index number to get
end (int, optional): Ending index number to get. If none specified, will be end of item. Defaults to None.
Returns:
Chepy: The Chepy object.
"""
if end is None:
self.state = self.state[start:]
else:
self.state = self.state[start:end]
return self

@ChepyDecorators.call_stack
def length(self) -> DataFormatT:
"""Get the length of the current state as string
Returns:
Chepy: The Chepy object.
"""
self.state = len(self.state)
return self
3 changes: 3 additions & 0 deletions chepy/modules/dataformat.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,6 @@ class DataFormat(ChepyCore):
def trim(self: DataFormatT) -> DataFormatT: ...
def convert_to_nato(self: DataFormatT, join_by:str) -> DataFormatT: ...
def swap_strings(self: DataFormatT, by:int) -> DataFormatT: ...
def to_string(self: DataFormatT) -> DataFormatT: ...
def select(self: DataFormatT, start: int, end: int) -> DataFormatT: ...
def length(self: DataFormatT) -> DataFormatT: ...
1 change: 1 addition & 0 deletions chepy/modules/networking.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ def get_ssl_cert(self, port: int = 443) -> NetworkingT:
domain = re.sub("^\w+://", "", self._convert_to_str())
with socket.create_connection((domain, port)) as sock:
context = ssl.create_default_context()
context.check_hostname = False
with context.wrap_socket(sock, server_hostname=domain) as sslsock:
cert = sslsock.getpeercert()
final = {}
Expand Down
7 changes: 7 additions & 0 deletions tests/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -224,3 +224,10 @@ def test_load_from_url():
)
== bytes
)


def test_for_each():
assert Chepy(["41", "42"]).for_each([("from_hex",), ("to_hex",)]).o == [
b"41",
b"42",
]
13 changes: 13 additions & 0 deletions tests/test_dataformat.py
Original file line number Diff line number Diff line change
Expand Up @@ -352,3 +352,16 @@ def test_nato_convert():

def test_swap_strings():
assert Chepy("oY u").swap_strings(2).o == "You "


def test_to_string():
assert Chepy(1).to_string().o == "1"


def test_select():
assert Chepy("abcd").select(0, 2).o == "ab"
assert Chepy("abcd").select(2).o == "cd"


def test_length():
assert Chepy("abcd").length().o == 4

0 comments on commit 264a542

Please sign in to comment.