Skip to content

Commit

Permalink
🗓 Aug 10, 2022 7:27:55 PM
Browse files Browse the repository at this point in the history
🔥 remove output method from core
🔥 change out to a property
🔥 remove out_as_str and out_as_bytes methods from core
🔍 option to use utc for from_unix_timestamp
🧪 tests added/updated
📔 docs added/updated
  • Loading branch information
securisec committed Aug 10, 2022
1 parent b99d240 commit 7c6355d
Show file tree
Hide file tree
Showing 20 changed files with 161 additions and 136 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/tests_multi_os.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ jobs:
# ${{ runner.os }}-pip-

- name: Install
# if: steps.devcache.outputs.cache-hit != 'true'
# if: steps.devcache.outs.cache-hit != 'true'
run: |
git submodule update --init --recursive
pip install -r requirements.txt
Expand Down
3 changes: 3 additions & 0 deletions TODO
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ New ideas:
☐ @high disable plugins from code
☐ homophonic decoder
☐ append method for core to add data to the state
☐ qr create
☐ random from state
☐ subsection. regex select from state and run all subsequent operations on the selected data

Bug:

Expand Down
35 changes: 4 additions & 31 deletions chepy/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ def __str__(self):
logging.exception(
"\n\nCannot print current state. Either chain with "
"another method, or use one of the output methods "
"Example: .o, .output, .state or .out()\n\n"
"Example: .o, .out, .state or .out\n\n"
)
return ""

Expand Down Expand Up @@ -549,7 +549,7 @@ def _convert_to_str(self) -> str:
return str(self.state)
elif isinstance(self.state, bytearray):
return bytearray(self.state).decode()
elif isinstance(self.state, float): # pragma: no cover
elif isinstance(self.state, float): # pragma: no cover
return format(self.state, "f")
else: # pragma: no cover
# todo check more types here
Expand Down Expand Up @@ -582,15 +582,6 @@ def o(self):
return self.state

@property
def output(self):
"""Get the final output
Returns:
Any: Final output
"""
return self.state

@ChepyDecorators.call_stack
def out(self) -> Any:
"""Get the final output
Expand All @@ -599,24 +590,6 @@ def out(self) -> Any:
"""
return self.state

@ChepyDecorators.call_stack
def out_as_str(self) -> str:
"""Get current value as str
Returns:
str: Current value as a string
"""
return self._convert_to_str()

@ChepyDecorators.call_stack
def out_as_bytes(self) -> bytes:
"""Get current value as bytes
Returns:
bytes: Current value as bytes
"""
return self._convert_to_bytes()

@ChepyDecorators.call_stack
def get_by_index(self, index: int):
"""Get an item by specifying an index
Expand Down Expand Up @@ -968,7 +941,7 @@ def save_recipe(self, path: str):
Examples:
>>> c = Chepy("some data").to_hex().base64_encode()
>>> c.save_recipe("/path/to/recipe)
>>> c.out()
>>> c.out
NzM2ZjZkNjUyMDY0NjE3NDYx
"""
with self._abs_path(path) as f:
Expand All @@ -986,7 +959,7 @@ def load_recipe(self, path: str):
Chepy: The Chepy object.
Examples:
>>> c = Chepy("some data").load_recipe("/path/to/recipe").out()
>>> c = Chepy("some data").load_recipe("/path/to/recipe").out
NzM2ZjZkNjUyMDY0NjE3NDYx
"""
with self._abs_path(path) as f:
Expand Down
2 changes: 1 addition & 1 deletion chepy/modules/compression.py
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@ def zlib_decompress(self) -> CompressionT:
>>> c = Chepy("789c0580a10d000008c35ee1b9ca05c104e737b761ca5711e8039a")
>>> c.hex_to_binary()
>>> c.zlib_decompress()
>>> c.out()
>>> c.out
b"some text"
"""
self.state = zlib.decompress(self._convert_to_bytes())
Expand Down
26 changes: 14 additions & 12 deletions chepy/modules/dataformat.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ def base58_encode(self) -> DataFormatT:
Chepy: The Chepy object.
Examples:
>>> Chepy("some data").base58_encode().output.decode()
>>> Chepy("some data").base58_encode().out.decode()
"2UDrs31qcWSPi"
"""
self.state = base58.b58encode(self._convert_to_bytes())
Expand All @@ -208,7 +208,7 @@ def base58_decode(self) -> DataFormatT:
Chepy: The Chepy object.
Examples:
>>> Chepy("2UDrs31qcWSPi").base58_decode().output.decode()
>>> Chepy("2UDrs31qcWSPi").base58_decode().out.decode()
"some data"
"""
self.state = base58.b58decode(self.state)
Expand All @@ -227,7 +227,7 @@ def base85_encode(self) -> DataFormatT:
Chepy: The Chepy object.
Examples:
>>> Chepy("some data").base85_encode().output.decode()
>>> Chepy("some data").base85_encode().out.decode()
"F)Po,+Cno&@/"
"""
self.state = base64.a85encode(self._convert_to_bytes())
Expand All @@ -246,7 +246,7 @@ def base85_decode(self) -> DataFormatT:
Chepy: The Chepy object.
Examples:
>>> Chepy("F)Po,+Cno&@/").base85_decode().output.decode()
>>> Chepy("F)Po,+Cno&@/").base85_decode().out.decode()
"some data"
"""
self.state = base64.a85decode(self._convert_to_bytes())
Expand Down Expand Up @@ -285,7 +285,7 @@ def base32_encode(self) -> DataFormatT:
Chepy: The Chepy object.
Examples:
>>> Chepy("some data").base32_encode().output.decode()
>>> Chepy("some data").base32_encode().out.decode()
"ONXW2ZJAMRQXIYI="
"""
self.state = base64.b32encode(self._convert_to_bytes())
Expand Down Expand Up @@ -438,7 +438,9 @@ def base64_encode(self, custom: str = None, url_safe: bool = False) -> DataForma
b'IqxhNG/YMLFV'
"""
if url_safe:
self.state = base64.urlsafe_b64encode(self._convert_to_bytes())
self.state = base64.urlsafe_b64encode(self._convert_to_bytes()).replace(
b"=", b""
)
return self
if custom is not None:
x = base64.b64encode(self._convert_to_bytes())
Expand Down Expand Up @@ -473,7 +475,7 @@ def base64_decode(self, custom: str = None, url_safe: bool = False) -> DataForma
Base64 decode using a custom string
>>> c = Chepy("QqxhNG/mMKtYPqoz64FVR42=")
>>> c.base64_decode(custom="./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz")
>>> c.out()
>>> c.out
b"some random? data"
"""
data = self._convert_to_str()
Expand Down Expand Up @@ -513,7 +515,7 @@ def to_hex(self, delimiter: str = "") -> DataFormatT:
Chepy: The Chepy object.
Examples:
>>> Chepy("AAA").to_hex().out().decode()
>>> Chepy("AAA").to_hex().out.decode()
"414141"
"""
if delimiter == "":
Expand All @@ -534,7 +536,7 @@ def from_hex(self, delimiter: str = None, join_by: str = " ") -> DataFormatT:
Chepy: The Chepy object.
Examples:
>>> Chepy("414141").from_hex().out()
>>> Chepy("414141").from_hex().out
b"AAA"
"""
if delimiter is not None:
Expand All @@ -558,12 +560,12 @@ def hex_to_int(self) -> DataFormatT:
Examples:
Chepy works with hex characters that start with a 0x
>>> Chepy("0x123").hex_to_int().output
>>> Chepy("0x123").hex_to_int().out
291
Without 0x in the hex
>>> Chepy("123").hex_to_int().output
>>> Chepy("123").hex_to_int().out
291
"""
if self._convert_to_str().startswith("0x"):
Expand Down Expand Up @@ -1304,7 +1306,7 @@ def swap_endianness(self) -> DataFormatT:
"""
data = self._convert_to_bytes()
# check if hex
if not re.match(b"^[0-9a-fA-F]+$", data): # pragma: no cover
if not re.match(b"^[0-9a-fA-F]+$", data): # pragma: no cover
raise ValueError("Data is not hex")
self.state = hex(struct.unpack("<I", struct.pack(">I", int(data, 16)))[0])[2:]
return self
9 changes: 7 additions & 2 deletions chepy/modules/datetimemodule.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,12 @@ def __init__(self, *data):
super().__init__(*data)

@ChepyDecorators.call_stack
def from_unix_timestamp(self, format: str='%c') -> DateTimeT:
def from_unix_timestamp(self, format: str = "%c", utc: bool = False) -> DateTimeT:
"""Convert UNIX timestamp to datetime
Args:
format: Format to use for datetime.strftime()
utc: Whether to use UTC or local timezone
Returns:
Chepy: The Chepy object.
Expand All @@ -25,7 +26,11 @@ def from_unix_timestamp(self, format: str='%c') -> DateTimeT:
>>> Chepy("1573426649").from_unix_timestamp()
"Sun Nov 10 17:57:29 2019"
"""
self.state = datetime.fromtimestamp(self._convert_to_int()).strftime(format)
data = self._convert_to_int()
if utc:
self.state = datetime.utcfromtimestamp(data).strftime(format)
else:
self.state = datetime.fromtimestamp(data).strftime(format)
return self

@ChepyDecorators.call_stack
Expand Down
2 changes: 1 addition & 1 deletion chepy/modules/datetimemodule.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ DateTimeT = TypeVar("DateTimeT", bound="DateTime")
class DateTime(ChepyCore):
def __init__(self, *data: Any) -> None: ...
state: Any = ...
def from_unix_timestamp(self: DateTimeT, format: str = ...) -> DateTimeT: ...
def from_unix_timestamp(self: DateTimeT, format: str = ..., utc: bool = ...) -> DateTimeT: ...
def to_unix_timestamp(self: DateTimeT) -> DateTimeT: ...
4 changes: 2 additions & 2 deletions chepy/modules/encryptionencoding.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ def rotate(self, rotate_by: int) -> EncryptionEncodingT:
Examples:
In this example, we will rotate by 20
>>> Chepy("some data").rotate(20).output
>>> Chepy("some data").rotate(20).out
"migy xunu"
"""
lc = string.ascii_lowercase
Expand Down Expand Up @@ -162,7 +162,7 @@ def rot_47(self, amount: int = 14) -> EncryptionEncodingT:
Chepy: The Chepy object.
Examples:
>>> Chepy("some").rot_47().output
>>> Chepy("some").rot_47().out
"D@>6"
"""
x = []
Expand Down
18 changes: 9 additions & 9 deletions chepy/modules/hashing.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def sha1(self) -> HashingT:
Chepy: The Chepy object.
Examples:
>>> Chepy("A").sha1().output
>>> Chepy("A").sha1().out
"6dcd4ce23d88e2ee9568ba546c007c63d9131c1b"
"""
self.state = hashlib.sha1(self._convert_to_bytes()).hexdigest()
Expand All @@ -73,7 +73,7 @@ def sha2_256(self) -> HashingT:
Chepy: The Chepy object.
Examples:
>>> Chepy("A").sha2_256().output
>>> Chepy("A").sha2_256().out
"559aead08264d5795d3909718cdd05abd49572e84fe55590eef31a88a08fdffd"
"""
self.state = hashlib.sha256(self._convert_to_bytes()).hexdigest()
Expand All @@ -96,7 +96,7 @@ def sha2_512(self) -> HashingT:
Chepy: The Chepy object.
Examples:
>>> Chepy("A").sha2_512().out()
>>> Chepy("A").sha2_512().out
21b4f4bd9e64ed355c3eb676a28ebedaf6d8f17bdc365995b319097153044080516bd083bfcce66121a3072646994c8430cc382b8dc543e84880183bf856cff5
"""
self.state = hashlib.sha512(self._convert_to_bytes()).hexdigest()
Expand Down Expand Up @@ -162,7 +162,7 @@ def sha2_224(self) -> HashingT:
Chepy: The Chepy object.
Examples:
>>> Chepy("A").sha2_224().out()
>>> Chepy("A").sha2_224().out
"5cfe2cddbb9940fb4d8505e25ea77e763a0077693dbb01b1a6aa94f2"
"""
self.state = hashlib.sha224(self._convert_to_bytes()).hexdigest()
Expand Down Expand Up @@ -279,7 +279,7 @@ def md5(self) -> HashingT:
Chepy: The Chepy object.
Examples:
>>> Chepy("A").md5().output
>>> Chepy("A").md5().out
"7fc56270e7a70fa81a5935b72eacbe29"
"""
h = MD5.new()
Expand Down Expand Up @@ -426,7 +426,7 @@ def blake_2b(self, bits: int = 256, key: bytes = "") -> HashingT:
Chepy: The Chepy object.
Examples:
>>> Chepy("A").blake_2b(bits=128, key="key").output
>>> Chepy("A").blake_2b(bits=128, key="key").out
"6d2e4cba3bc564e02d1a76f585a6795d"
"""
assert bits in [
Expand Down Expand Up @@ -458,7 +458,7 @@ def blake_2s(self, bits: int = 256, key: bytes = "") -> HashingT:
Chepy: The Chepy object.
Examples:
>>> Chepy("A").blake_2s(bits=128, key="key").output
>>> Chepy("A").blake_2s(bits=128, key="key").out
"4e33cc702e9d08c28a5e9691f23bc66a"
"""
assert bits in [256, 160, 128], "Valid bits are 256, 160, 128"
Expand Down Expand Up @@ -507,7 +507,7 @@ def crc32_checksum(self) -> HashingT:
Chepy: The Chepy object.
Examples:
>>> Chepy("a").crc32_checksum().output
>>> Chepy("a").crc32_checksum().out
"e8b7be43"
"""
self.state = Crc32().process(self._convert_to_bytes()).finalhex()
Expand Down Expand Up @@ -692,7 +692,7 @@ def scrypt_hash(
Chepy: The Chepy object.
Examples:
>>> Chepy("abc").scrypt_hash(salt="", key_length=16).out()
>>> Chepy("abc").scrypt_hash(salt="", key_length=16).out
"f352f3374cf4e344dde4108b96985248"
"""
assert N < 32, "N must be less than 32"
Expand Down
4 changes: 2 additions & 2 deletions chepy/modules/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def reverse(self, count: int = 1) -> UtilsT:
Chepy: The Chepy object.
Examples:
>>> Chepy("abcdefg").reverse().output
>>> Chepy("abcdefg").reverse().out
"gfedcba"
"""
if count == 1:
Expand Down Expand Up @@ -58,7 +58,7 @@ def count_occurances(self, regex: str, case_sensitive: bool = False) -> UtilsT:
Chepy: The Chepy object.
Examples:
>>> Chepy("AABCDADJAKDJHKSDAJSDdaskjdhaskdjhasdkja").count_occurances("ja").output
>>> Chepy("AABCDADJAKDJHKSDAJSDdaskjdhaskdjhasdkja").count_occurances("ja").out
2
"""
if case_sensitive:
Expand Down

0 comments on commit 7c6355d

Please sign in to comment.