Skip to content

Commit

Permalink
Nov 2, 2021 10:49 PM
Browse files Browse the repository at this point in the history
✨ to_leetcode method
✨ split_lines method
✨ new constants for leetcode convert
🧪 tests added/updated
  • Loading branch information
securisecctf committed Nov 3, 2021
1 parent 3acc182 commit 4ffe000
Show file tree
Hide file tree
Showing 9 changed files with 96 additions and 39 deletions.
41 changes: 3 additions & 38 deletions TODO
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@ New ideas:
☐ swap little and big endian
☐ ignore error method
☐ swap bytes
☐ split by newline
☐ @high disable plugins from code
☐ string to leetcode

Bug:

Expand Down Expand Up @@ -52,39 +50,6 @@ 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)
✔ strip ansi color codes @project(New ideas)
✔ build pyinstaller artifact for osx, linux, windows @project(Github Actions)
✔ @high from morse word delimiter is broken - .... . / ..-. .-.. .- --. / .. ... / --- .--- --.- -..- . -.-- ...-- ..- -- --.. ..... .-- .. -- .-.. . .-.. ..... ....- - .- ..... -.- --... --- .- --.. - --. ..--- ..--- --... --. -... --.. ..-. -.... -. .-.. --.- .--. . --... - -.... .--. --.. --... .-.. ..... - --. -.-. -. -.. -... -- -- ...-- -.. .- -. .-.. ..... / -... .- ... . ...-- ..--- @project(Bug)
✔ ecc generate @project(New ideas)
✔ strip/replace method @project(New ideas)
✔ convert to nato alphabets: a = alpha, b = bravo @project(New ideas)
✔ rsa sign/verify @project(New ideas)
✔ rsa generate random key (private and public) @project(New ideas)
✔ rsa encrypt/decrpt @project(New ideas)
✔ sqlite plugin @project(Plugins)
✔ base 16 @project(Methods)
✔ protobuf plugin @project(Plugins)
✔ string to l33tspeak. @project(Methods)
✔ run script loaded into chepy. script should be a function that takes one arg, and state is passed to arg. fixed function name in script should be passed that can be imported on method call @project(New ideas)
✔ use $EDITOR to open state in an editor https://github.com/fmoo/python-editor. @project(Cli)
✔ run command method to save output to state @project(New ideas)
✔ use lazy-import to increase performance @project(Code)
✔ rotate bruteforce @project(New ideas)
✔ derive pbkdf2 key @project(New ideas)
✔ fix zip header @project(New ideas)
✘ switch to mypy as linter @project(Code)
✔ load_file with binary flag @project(Enhance)
✔ incorrect padding in base64 @project(Enhance)
✔ optionally disable with arg @project(Enhance)
✔ exrex for regex based generation. arg for all combos @project(Methods)
✔ raw inflate and deflate @project(Methods)
✔ extract base64 method @project(Methods)
✔ sort dict by key or value @project(Methods)
✔ Change sort list to be able to sort by any data type @project(Methods)
✔ loop method to loop over the same thing @project(Methods)
✔ get help for a method with ? @project(Cli)
✔ allow comments in cli @project(Cli)
✔ split by newline @project(New ideas)
✔ string to leetcode @project(New ideas)

31 changes: 31 additions & 0 deletions chepy/modules/dataformat.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import html
import base58
import json
from random import randint

yaml = lazy_import.lazy_module("yaml")
import regex as re
Expand Down Expand Up @@ -1120,3 +1121,33 @@ def length(self) -> DataFormatT:
"""
self.state = len(self.state)
return self

@ChepyDecorators.call_stack
def to_leetcode(self, replace_space: str = "") -> DataFormatT:
"""Convert to leetcode. Reference
Reference github.com/ss8651twtw/CTF-flag-generator
Args:
replace_space (str, optional): Replace spaces with specified char. Defaults to ''.
Returns:
Chepy: The Chepy object.
"""

def change(c):
if replace_space and c == " ":
return replace_space
if c.isalpha():
c = c.upper()
char_set = Encoding.LEETCODE[ord(c) - ord("A")]
new_c = char_set[randint(0, len(char_set) - 1)]
return new_c
else:
return c

hold = ""
string = self._convert_to_str()
for s in string:
hold += change(s)
self.state = hold
return self
1 change: 1 addition & 0 deletions chepy/modules/dataformat.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,4 @@ class DataFormat(ChepyCore):
def to_string(self: DataFormatT) -> DataFormatT: ...
def select(self: DataFormatT, start: int, end: int) -> DataFormatT: ...
def length(self: DataFormatT) -> DataFormatT: ...
def to_leetcode(self: DataFormatT, replace_space: str=...) -> DataFormatT: ...
29 changes: 29 additions & 0 deletions chepy/modules/internal/constants.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,33 @@
class Encoding(object):
LEETCODE = char_map = [
["A", "a", "4", "@"],
["B", "b", "8"],
["C", "c"],
["D", "d"],
["E", "e", "3"],
["F", "f"],
["G", "g", "6", "9"],
["H", "h"],
["I", "i", "1"],
["J", "j"],
["K", "k"],
["L", "l", "1"],
["M", "m"],
["N", "n"],
["O", "o", "0"],
["P", "p"],
["Q", "q"],
["R", "r"],
["S", "s", "5", "$"],
["T", "t", "7"],
["U", "u"],
["V", "v"],
["W", "w"],
["X", "x"],
["Y", "y"],
["Z", "z", "2"],
]

NATO_CONSTANTS_DICT = {
"A": "Alpha",
"B": "Bravo",
Expand Down
3 changes: 2 additions & 1 deletion chepy/modules/internal/constants.pyi
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
from typing import Any, Dict
from typing import Any, Dict, List

class Encoding:
py_encodings: Any = ...
py_text_encodings: Any = ...
asciichars: Any = ...
brailles: Any = ...
NATO_CONSTANTS_DICT: Dict[str, str] = ...
LEETCODE: List[List[str]] = ...

class EncryptionConsts:
MORSE_CODE_DICT: Any = ...
Expand Down
11 changes: 11 additions & 0 deletions chepy/modules/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from typing import TypeVar, Union

import chepy.modules.internal.colors as _int_colors

exrex = lazy_import.lazy_module("exrex")
import pydash
import regex as re
Expand Down Expand Up @@ -202,6 +203,16 @@ def split_by_n(self, n: int) -> UtilsT:
self.state = re.findall(".{1," + str(n) + "}", self._convert_to_str())
return self

@ChepyDecorators.call_stack
def split_lines(self):
"""Split a string by newline characters.
Returns:
Chepy: The Chepy object.
"""
self.state = self._convert_to_str().split()
return self

@ChepyDecorators.call_stack
def select_every_n(self, n: int, start: int = 0) -> UtilsT:
"""Select every nth item from a list or string.
Expand Down
1 change: 1 addition & 0 deletions chepy/modules/utils.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class Utils(ChepyCore):
def split_by_char(self: UtilsT, delimiter: str=...) -> UtilsT: ...
def split_by_regex(self: UtilsT, pattern: str=..., trim: Any=...) -> UtilsT: ...
def split_by_n(self: UtilsT, n: int) -> UtilsT: ...
def split_lines(self: UtilsT) -> UtilsT: ...
def select_every_n(self: UtilsT, n: int, start: int=...) -> UtilsT: ...
def unique(self: UtilsT) -> UtilsT: ...
def sort_list(self: UtilsT, reverse: bool=...) -> UtilsT: ...
Expand Down
4 changes: 4 additions & 0 deletions tests/test_dataformat.py
Original file line number Diff line number Diff line change
Expand Up @@ -365,3 +365,7 @@ def test_select():

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


def test_leetcode():
assert len(Chepy("ab@ cd").to_leetcode(replace_space="_").o) == 6
14 changes: 14 additions & 0 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,20 @@ def test_split_by_n():
assert Chepy("some string").split_by_n(2).o[2] == " s"


def test_split_lines():
assert (
len(
Chepy(
"""hello
world"""
)
.split_lines()
.o
)
== 2
)


def test_select_n():
assert Chepy(["a", 1, "lol", "", True]).select_every_n(3).o == ["a", ""]

Expand Down

0 comments on commit 4ffe000

Please sign in to comment.