cryptosploit_modules.encodings.base.base16

View Source
 0from base64 import b16encode, b16decode
 1from binascii import Error
 2from os.path import isfile
 3
 4from cryptosploit.cprint import Printer
 5from cryptosploit.exceptions import ArgError
 6from cryptosploit_modules import BaseModule
 7
 8
 9class Base16(BaseModule):
10    def __init__(self):
11        super().__init__()
12        self.env.check_var = self.check_var
13
14    @staticmethod
15    def check_var(name, value):
16        match name:
17            case "mode":
18                if value in ("encode", "decode"):
19                    return True, ""
20                return False, "May be decode/encode"
21            case _:
22                return True, ""
23
24    def encode_command(self, inp):
25        text = b16encode(inp.encode()).decode()
26        Printer.positive("Encoded string:\n" + text)
27
28    def decode_command(self, inp):
29        try:
30            text = b16decode(inp.encode()).decode()
31        except (ValueError, Error) as err:
32            raise ArgError("Your input is not valid base85 string") from err
33        else:
34            Printer.positive("Decoded string:\n" + text)
35
36    def run(self):
37        inp = self.env.get_var("input").value
38        if isfile(inp):
39            with open(inp) as f:
40                inp = f.read()
41        if inp:
42            func = getattr(self, self.env.get_var("mode").value + "_command")
43            return func(inp)
44        raise ArgError("All variables must be set")
45
46
47module = Base16
View Source
10class Base16(BaseModule):
11    def __init__(self):
12        super().__init__()
13        self.env.check_var = self.check_var
14
15    @staticmethod
16    def check_var(name, value):
17        match name:
18            case "mode":
19                if value in ("encode", "decode"):
20                    return True, ""
21                return False, "May be decode/encode"
22            case _:
23                return True, ""
24
25    def encode_command(self, inp):
26        text = b16encode(inp.encode()).decode()
27        Printer.positive("Encoded string:\n" + text)
28
29    def decode_command(self, inp):
30        try:
31            text = b16decode(inp.encode()).decode()
32        except (ValueError, Error) as err:
33            raise ArgError("Your input is not valid base85 string") from err
34        else:
35            Printer.positive("Decoded string:\n" + text)
36
37    def run(self):
38        inp = self.env.get_var("input").value
39        if isfile(inp):
40            with open(inp) as f:
41                inp = f.read()
42        if inp:
43            func = getattr(self, self.env.get_var("mode").value + "_command")
44            return func(inp)
45        raise ArgError("All variables must be set")
#   Base16()
View Source
11    def __init__(self):
12        super().__init__()
13        self.env.check_var = self.check_var
#  
@staticmethod
def check_var(name, value):
View Source
15    @staticmethod
16    def check_var(name, value):
17        match name:
18            case "mode":
19                if value in ("encode", "decode"):
20                    return True, ""
21                return False, "May be decode/encode"
22            case _:
23                return True, ""
#   def encode_command(self, inp):
View Source
25    def encode_command(self, inp):
26        text = b16encode(inp.encode()).decode()
27        Printer.positive("Encoded string:\n" + text)
#   def decode_command(self, inp):
View Source
29    def decode_command(self, inp):
30        try:
31            text = b16decode(inp.encode()).decode()
32        except (ValueError, Error) as err:
33            raise ArgError("Your input is not valid base85 string") from err
34        else:
35            Printer.positive("Decoded string:\n" + text)
#   def run(self):
View Source
37    def run(self):
38        inp = self.env.get_var("input").value
39        if isfile(inp):
40            with open(inp) as f:
41                inp = f.read()
42        if inp:
43            func = getattr(self, self.env.get_var("mode").value + "_command")
44            return func(inp)
45        raise ArgError("All variables must be set")

Required to be overridden in the child class. Function called by the user

View Source
10class Base16(BaseModule):
11    def __init__(self):
12        super().__init__()
13        self.env.check_var = self.check_var
14
15    @staticmethod
16    def check_var(name, value):
17        match name:
18            case "mode":
19                if value in ("encode", "decode"):
20                    return True, ""
21                return False, "May be decode/encode"
22            case _:
23                return True, ""
24
25    def encode_command(self, inp):
26        text = b16encode(inp.encode()).decode()
27        Printer.positive("Encoded string:\n" + text)
28
29    def decode_command(self, inp):
30        try:
31            text = b16decode(inp.encode()).decode()
32        except (ValueError, Error) as err:
33            raise ArgError("Your input is not valid base85 string") from err
34        else:
35            Printer.positive("Decoded string:\n" + text)
36
37    def run(self):
38        inp = self.env.get_var("input").value
39        if isfile(inp):
40            with open(inp) as f:
41                inp = f.read()
42        if inp:
43            func = getattr(self, self.env.get_var("mode").value + "_command")
44            return func(inp)
45        raise ArgError("All variables must be set")
#   module()
View Source
11    def __init__(self):
12        super().__init__()
13        self.env.check_var = self.check_var
#  
@staticmethod
def check_var(name, value):
View Source
15    @staticmethod
16    def check_var(name, value):
17        match name:
18            case "mode":
19                if value in ("encode", "decode"):
20                    return True, ""
21                return False, "May be decode/encode"
22            case _:
23                return True, ""
#   def encode_command(self, inp):
View Source
25    def encode_command(self, inp):
26        text = b16encode(inp.encode()).decode()
27        Printer.positive("Encoded string:\n" + text)
#   def decode_command(self, inp):
View Source
29    def decode_command(self, inp):
30        try:
31            text = b16decode(inp.encode()).decode()
32        except (ValueError, Error) as err:
33            raise ArgError("Your input is not valid base85 string") from err
34        else:
35            Printer.positive("Decoded string:\n" + text)
#   def run(self):
View Source
37    def run(self):
38        inp = self.env.get_var("input").value
39        if isfile(inp):
40            with open(inp) as f:
41                inp = f.read()
42        if inp:
43            func = getattr(self, self.env.get_var("mode").value + "_command")
44            return func(inp)
45        raise ArgError("All variables must be set")

Required to be overridden in the child class. Function called by the user