How to use the ciphey.iface.Decoder function in ciphey

To help you get started, weโ€™ve selected a few ciphey examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github brandonskerritt / Ciphey / ciphey / basemods / Decoders / octal.py View on Github external
from typing import Optional, Dict, Any

from loguru import logger

import ciphey
from ciphey.iface import registry


@registry.register
class Octal(ciphey.iface.Decoder[str, bytes]):
    @staticmethod
    def getTarget() -> str:
        return "octal"

    def decode(self, text: str) -> Optional[bytes]:
        """
        It takes an octal string and return a string
            :octal_str: octal str like "110 145 154"
        """
        str_converted = []
        octal_seq = text.split(" ")
        if len(octal_seq) == 1:
            # Concatted octal must be formed of octal triplets
            if len(text) % 3 != 0:
                return None
            octal_seq = [text[i:i+3] for i in range(0, len(text), 3)]
github brandonskerritt / Ciphey / ciphey / basemods / Decoders / bases.py View on Github external
"base85": (base64.b85decode, 0.01),
    "ascii85": (base64.a85decode, 0.1),
}


def gen_class(name, decoder, priority, ns):
    ns["_get_func"] = ciphey.common.id_lambda(decoder)
    ns["decode"] = lambda self, ctext: _dispatch(self, ctext, self._get_func())
    ns["getParams"] = ciphey.common.id_lambda(None)
    ns["getTarget"] = ciphey.common.id_lambda(name)
    ns["priority"] = ciphey.common.id_lambda(priority)
    ns["__init__"] = lambda self, config: super(type(self), self).__init__(config)


for name, (decoder, priority) in _bases.items():
    t = types.new_class(name, (ciphey.iface.Decoder[str, bytes],),
                        exec_body=lambda x: gen_class(name, decoder, priority, x))

    ciphey.iface.registry.register(t)
github brandonskerritt / Ciphey / ciphey / basemods / Decoders / unicode.py View on Github external
from typing import Optional, Dict, Any

from loguru import logger

import ciphey
from ciphey.iface import registry


@registry.register
class Utf8(ciphey.iface.Decoder[bytes, str]):
    @staticmethod
    def getTarget() -> str:
        return "utf8"

    def decode(self, text: bytes) -> Optional[str]:
        logger.trace("Attempting utf-8 decode")
        try:
            res = text.decode("utf8")
            logger.debug(f"utf-8 decode gave '{res}'")
            return res if len(res) != 0 else None
        except UnicodeDecodeError:
            logger.trace("utf-8 decode failed")
            return None

    @staticmethod
    def getParams() -> Optional[Dict[str, Dict[str, Any]]]:
github brandonskerritt / Ciphey / ciphey / basemods / Searchers / ausearch.py View on Github external
self, target: Any
    ) -> (bool, Union[Tuple[SearchLevel, str], List[SearchLevel]]):
        """
            If there exists a decoding that the checker returns true on, returns (True, result).
            Otherwise, returns (False, names and successful decodings)

            The CrackResult object should only have the value field filled in

            MUST NOT recurse into decodings! evaluate does that for you!
        """
        # This tag is necessary, as we could have a list as a decoding target, which would then screw over type checks
        ret = []

        decoders = []

        for decoder_type, decoder_class in registry[Decoder][type(target)].items():
            for decoder in decoder_class:
                decoders.append(DecoderComparer(decoder))
        # Fun fact:
        #     with Python's glorious lists, inserting n elements into the right position (with bisect) is O(n^2)
        decoders.sort(reverse=True)

        for decoder_cmp in decoders:
            logger.trace(f"Inspecting {decoder_cmp}")
            res = self._config()(decoder_cmp.value).decode(target)

            if res is None:
                continue
            level = SearchLevel(
                name=decoder_cmp.value.__name__.lower(),
                result=CrackResult(value=res),
            )
github brandonskerritt / Ciphey / ciphey / basemods / Decoders / morse.py View on Github external
from typing import Optional, Dict, Any, List
import re
from loguru import logger
import ciphey
from ciphey.iface import registry


@registry.register
class MorseCode(ciphey.iface.Decoder[str, str]):
    # A priority list for char/word boundaries
    BOUNDARIES = {" ": 1, "/": 2, "\n": 3, ".": -1, "-": -1}
    MAX_PRIORITY = 3
    ALLOWED = {".", "-", " ", "/", "\n"}
    MORSE_CODE_DICT: Dict[str, str]
    MORSE_CODE_DICT_INV: Dict[str, str]

    @staticmethod
    def getTarget() -> str:
        return "morse"

    def decode(self, text: str) -> Optional[str]:
        # Trim end
        while text[-1] in self.BOUNDARIES:
            text = text[:-1]
github brandonskerritt / Ciphey / ciphey / basemods / Decoders / reverse.py View on Github external
from typing import Optional, Dict, List

from ciphey.iface import ParamSpec, Config, T, U, Decoder, registry


@registry.register_multi((str, str), (bytes, bytes))
class Reverse(Decoder):
    def decode(self, ctext: T) -> Optional[U]:
        return ctext[::-1]

    @staticmethod
    def priority() -> float:
        return 0.05

    def __init__(self, config: Config):
        super().__init__(config)

    @staticmethod
    def getParams() -> Optional[Dict[str, ParamSpec]]:
        pass

    @staticmethod
    def getTarget() -> str: