Skip to content

Commit

Permalink
🗓 Apr 11, 2024 9:59:42 PM
Browse files Browse the repository at this point in the history
🧪 tests added/updated
✨ bit_shift_right/left
🔥 remove str_bit_shift_right
  • Loading branch information
securisec committed Apr 12, 2024
1 parent 0960163 commit fefd6f9
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 23 deletions.
55 changes: 38 additions & 17 deletions chepy/modules/aritmeticlogic.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import binascii
import statistics
from typing import TypeVar, Union
from typing import TypeVar, Union, Literal
from functools import reduce as functools_reduce

from ..core import ChepyCore, ChepyDecorators
Expand All @@ -21,21 +21,6 @@ def __hex_to_int(self, n): # pragma: no cover
if isinstance(n, int):
return n

@ChepyDecorators.call_stack
def str_bit_shift_right(self, amount: int) -> AritmeticLogicT:
"""Bit shift string right
Args:
amount (int): Amount to shift
Returns:
Chepy: The Chepy object
"""
self.state = binascii.unhexlify(
"".join(list(format(ord(x) >> int(amount), "02x") for x in list("hello")))
)
return self

@ChepyDecorators.call_stack
def add(self, n: int) -> AritmeticLogicT:
"""Add a number to the state
Expand Down Expand Up @@ -82,7 +67,7 @@ def addition(self, delimiter=None) -> AritmeticLogicT:
Chepy: The Chepy object.
"""
data = self._convert_to_str()
print('🟢 ', data)
print("🟢 ", data)
if not delimiter:
delimiter = detect_delimiter(data)
# only work on numbers
Expand Down Expand Up @@ -249,3 +234,39 @@ def int_to_base(self, base: Union[int, str]) -> AritmeticLogicT:
"""
self.state = int(self.state, base)
return self

@ChepyDecorators.call_stack
def bit_shift_right(
self,
amount: int = 1,
operation_type: Literal["logical", "arithmetic"] = "logical",
) -> AritmeticLogicT:
"""Shifts the bits in each byte towards the right by the specified amount.
Args:
amount (int, optional): Amount. Defaults to 1
operation_type (Literal['logical', 'arithmetic'], optional): Operation type. Defaults to 'logical'.
Returns:
Chepy: The Chepy object.
"""
mask = 0x80 if operation_type.lower() != "logical" else 0
output_bytes = [
(byte >> int(amount)) ^ (byte & mask) for byte in self._convert_to_bytes()
]
self.state = bytearray(output_bytes)
return self

@ChepyDecorators.call_stack
def bit_shift_left(self, amount: int = 1):
"""Shifts each byte in the input byte array to the left by a specified amount.
Args:
amount (int, optional): Amount. Defaults to 1.
Returns:
Chepy: The Chepy object.
"""
output_bytes = [(byte << amount) & 0xFF for byte in self._convert_to_bytes()]
self.state = bytearray(output_bytes)
return self
5 changes: 3 additions & 2 deletions chepy/modules/aritmeticlogic.pyi
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
from ..core import ChepyCore
from typing import Any, TypeVar, Union
from typing import Any, TypeVar, Union, Literal

AritmeticLogicT = TypeVar("AritmeticLogicT", bound="AritmeticLogic")

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: Union[int, str]) -> AritmeticLogicT: ...
def addition(self: AritmeticLogicT, delimiter: Union[str, None]=None) -> AritmeticLogicT: ...
def sub(self: AritmeticLogicT, n: Union[int, str]) -> AritmeticLogicT: ...
Expand All @@ -18,3 +17,5 @@ class AritmeticLogic(ChepyCore):
def mean(self: AritmeticLogicT) -> AritmeticLogicT: ...
def median(self: AritmeticLogicT) -> AritmeticLogicT: ...
def int_to_base(self: AritmeticLogicT, base: Union[int, str]) -> AritmeticLogicT: ...
def bit_shift_right(self: AritmeticLogicT, amount: int=1, operation_type: Literal['logical', 'arithmetic']='logical') -> AritmeticLogicT: ...
def bit_shift_left(self: AritmeticLogicT, amount: int=1) -> AritmeticLogicT: ...
11 changes: 7 additions & 4 deletions tests/test_aritmeticlogic.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
from chepy import Chepy


def test_bit_shift_right():
assert Chepy("hello").str_bit_shift_right(2).to_hex().o == b"1a191b1b1b"


def test_add():
assert Chepy("40").add(1).to_int().o == 51
assert Chepy("hello").add("ff").o == b"gdkkn"
Expand Down Expand Up @@ -56,3 +52,10 @@ def test_addition():

def test_int_to_base():
assert Chepy("067165").int_to_base(8).o == 28277


def test_bitwise_operations():
assert Chepy("A").bit_shift_right(3).o == b"\x08"
assert Chepy(b"414243").bit_shift_right().o == b"\x1a\x18\x1a\x19\x1a\x19"
assert Chepy("A").bit_shift_left().o == b"\x82"
assert Chepy("414243").from_hex().bit_shift_left(7).o == b"\x80\x00\x80"

0 comments on commit fefd6f9

Please sign in to comment.