Exemple #1
0
    def gen_chk(cls, type: str, values: List[Union[int, str]]) -> str:
        """Generates a "chk" value, used in different requests to GD servers.

        The method is: combine_values -> add salt -> sha1 hash
        -> XOR -> Base64 encode -> return

        Parameters
        ----------
        type: :class:`str`
            String representation of type, e.g. ``'comment'``.
            Used to define salt and XOR key.

        values: List[Union[:class:`int`, :class:`str`]]
            List of values to generate a chk with.

        Returns
        -------
        :class:`str`
            Generated ``'chk'``, represented as string.
        """
        salt = cls.salts.get(type, "")  # get salt

        values.append(salt)

        string = "".join(map(str, values))

        # sha1 hash
        hashed = hashlib.sha1(string.encode()).hexdigest()
        # XOR
        xored = XOR.cipher(key=cls.keys[type], string=hashed)
        # Base64
        final = cls.do_base64(xored, encode=True)

        return final
Exemple #2
0
    def decode(cls, type: str, string: str) -> str:
        """Decodes a XOR -> Base64 ciphered string.

        .. note::
            Due to the fact that decode and encode work almost the same,
            the following is true:

            .. code-block:: python3

                Coder.decode('message', Coder.encode('message', 'NeKit')) == 'NeKit'  # True

        Parameters
        ----------
        type: :class:`str`
            String representation of a type, e.g. ``'level'``.
            Used to define a XOR key.

        string: :class:`str`
            String to decode.

        Returns
        -------
        :class:`str`
            Decoded string.
        """
        ciphered = cls.do_base64(string, encode=False)
        decoded = XOR.cipher(key=cls.keys[type], string=ciphered)
        return decoded
Exemple #3
0
    def decode(cls, type: str, string: str, use_bytes: bool = False) -> str:
        """Decodes a XOR -> Base64 ciphered string.

        .. note::
            Due to the fact that decode and encode work almost the same,
            the following is true:

            .. code-block:: python3

                Coder.decode('message', Coder.encode('message', 'NeKit')) == 'NeKit'  # True

        Parameters
        ----------
        type: :class:`str`
            String representation of a type, e.g. ``'level'``.
            Used to define a XOR key.

        string: :class:`str`
            String to decode.

        Returns
        -------
        :class:`str`
            Decoded string.
        """
        string += "=" * (4 - len(string) % 4)  # add padding

        try:
            cipher_stream = urlsafe_b64decode(string.encode())
        except Exception:
            return string

        if use_bytes:
            return XOR.cipher_bytes(key=cls.keys[type], stream=cipher_stream)
        else:
            return XOR.cipher(key=cls.keys[type],
                              string=cipher_stream.decode(errors="ignore"))
Exemple #4
0
    def encode(cls, type: str, string: str) -> str:
        """Encodes a string, combining XOR and Base64 encode methods.

        Used in different aspects of gd.py.

        Parameters
        ----------
        type: :class:`str`
            String representation of type, e.g. ``'levelpass'``.
            Used to define a XOR key.

        string: :class:`str`
            String to encode.

        Returns
        -------
        :class:`str`
            Encoded string.
        """
        ciphered = XOR.cipher(key=cls.keys[type], string=string)
        encoded = cls.do_base64(ciphered, encode=True)
        return encoded