Skip to content

Commit

Permalink
🗓 Feb 20, 2024 7:11:02 PM
Browse files Browse the repository at this point in the history
🐙 update add and subtract to align to cyberchef
🧪 tests added/updated
🐙 add _log to core
  • Loading branch information
securisec committed Feb 21, 2024
1 parent d459d84 commit 1ba98da
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 15 deletions.
2 changes: 2 additions & 0 deletions chepy/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,8 @@ def __init__(self, *data):
self.log_format = "%(levelname)-2s - %(message)s"
logging.getLogger().setLevel(self.log_level)
logging.basicConfig(format=self.log_format)
# logger
self._log = logging

@property
def recipe(self) -> List[Dict[str, Union[str, Dict[str, Any]]]]:
Expand Down
2 changes: 2 additions & 0 deletions chepy/core.pyi
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import logging
from typing import Any, List, Mapping, Tuple, Union, TypeVar, Literal, Callable, Dict

jsonpickle: Any
Expand All @@ -19,6 +20,7 @@ class ChepyCore:
read_file: Any = ...
log_level: Any = ...
log_format: str = ...
_log: logging.Logger = ...
def __init__(self, *data: Any) -> None: ...
def _convert_to_bytes(self) -> bytes: ...
def _to_bytes(self, data: Any) -> bytes: ...
Expand Down
62 changes: 51 additions & 11 deletions chepy/modules/aritmeticlogic.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,34 @@ def add(self, n: int) -> AritmeticLogicT:
"""Add a number to the state
Args:
n (int): Number to add with
n (int): Number to add with. Can be decimal or hex string without 0x
Returns:
Chepy: The Chepy object.
"""
if not isinstance(self.state, int):
self.state = self.__hex_to_int(self.state)
self.state = self.state + n
# Determine the base of the key (hexadecimal or decimal)
if isinstance(n, int):
# Try converting to decimal
key_int = n
else:
try:
# Try converting to hexadecimal
key_int = int(n, 16)
except ValueError: # pragma: no cover
self._log.error(
"Invalid key format. Must be a decimal or hexadecimal string."
)
return self

hold = b""
for char_code in self._convert_to_bytes():
# Add the key to the integer and take the result modulo 255
result_code = (char_code + key_int) % 256

# Convert the result back to a byte
hold += result_code.to_bytes(1, byteorder="big")

self.state = hold
return self

@ChepyDecorators.call_stack
Expand All @@ -59,9 +79,29 @@ def subtract(self, n: int) -> AritmeticLogicT:
Returns:
Chepy: The Chepy object.
"""
if not isinstance(self.state, int):
self.state = self.__hex_to_int(self.state)
self.state = self.state - n
# Determine the base of the key (hexadecimal or decimal)
if isinstance(n, int):
# Try converting to decimal
key_int = n
else:
try:
# Try converting to hexadecimal
key_int = int(n, 16)
except ValueError: # pragma: no cover
self._log.error(
"Invalid key format. Must be a decimal or hexadecimal string."
)
return self

hold = b""
for char_code in self._convert_to_bytes():
# Add the key to the integer and take the result modulo 255
result_code = (char_code - key_int) % 256

# Convert the result back to a byte
hold += result_code.to_bytes(1, byteorder="big")

self.state = hold
return self

@ChepyDecorators.call_stack
Expand All @@ -81,8 +121,8 @@ def multiply(self, n: int) -> AritmeticLogicT:

@ChepyDecorators.call_stack
def divide(self, n: int) -> AritmeticLogicT:
"""Divide a number to the state. Chepy is not optimized for float math.
Subsequent methods may fail.
"""Divide a number to the state. Chepy is not optimized for float math.
Subsequent methods may fail.
Args:
n (int): Number to divide with
Expand All @@ -107,7 +147,7 @@ def power(self, n: int) -> AritmeticLogicT:
"""
if not isinstance(self.state, int):
self.state = self.__hex_to_int(self.state)
self.state = self.state ** n
self.state = self.state**n
return self

@ChepyDecorators.call_stack
Expand Down Expand Up @@ -149,7 +189,7 @@ def median(self) -> AritmeticLogicT:
@ChepyDecorators.call_stack
def int_to_base(self, base: Union[int, str]) -> AritmeticLogicT:
"""Convert the state to a different base
Args:
base (int): Base to convert to
Expand Down
4 changes: 2 additions & 2 deletions chepy/modules/aritmeticlogic.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ class AritmeticLogic(ChepyCore):
def __init__(self, *data: Any) -> None: ...
state: Any = ...
def str_bit_shift_right(self: AritmeticLogicT, amount: int) -> AritmeticLogicT: ...
def add(self: AritmeticLogicT, n: int) -> AritmeticLogicT: ...
def subtract(self: AritmeticLogicT, n: int) -> AritmeticLogicT: ...
def add(self: AritmeticLogicT, n: Union[int, str]) -> AritmeticLogicT: ...
def subtract(self: AritmeticLogicT, n: Union[int, str]) -> AritmeticLogicT: ...
def multiply(self: AritmeticLogicT, n: int) -> AritmeticLogicT: ...
def divide(self: AritmeticLogicT, n: int) -> AritmeticLogicT: ...
def power(self: AritmeticLogicT, n: int) -> AritmeticLogicT: ...
Expand Down
9 changes: 7 additions & 2 deletions tests/test_aritmeticlogic.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ def test_bit_shift_right():


def test_add():
assert Chepy("0x40").add(1).o == 65
assert Chepy("40").add(1).to_int().o == 51
assert Chepy("hello").add('ff').o == b'gdkkn'
assert Chepy(9).add('01').o == b':'


def test_multiply():
Expand Down Expand Up @@ -37,7 +39,10 @@ def test_median():


def test_subtract():
assert Chepy(["0x02", "0x04"]).loop_list("subtract", {"n": 1}).o == [1, 3]
assert Chepy("40").subtract(1).o == b'3/'
assert Chepy("hello").subtract('10').o == b'XU\\\\_'
assert Chepy("hello").subtract(10).o == b'^[bbe'
# assert Chepy(9).add('01').o == b':'


def test_int_to_base():
Expand Down

0 comments on commit 1ba98da

Please sign in to comment.