Skip to content

Commit

Permalink
bug fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
sht2017 committed Jun 28, 2024
1 parent 78a9fae commit e983466
Show file tree
Hide file tree
Showing 13 changed files with 1,398 additions and 1,344 deletions.
7 changes: 0 additions & 7 deletions .vscode/settings.json

This file was deleted.

9 changes: 7 additions & 2 deletions iptv.code-workspace
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,12 @@
"settings": {
"pylint.cwd": "${workspaceFolder}/src",
"terminal.integrated.env.windows": {
"PYTHONPATH": "${env:PYTHONPATH};${workspaceFolder}/src"
}
"PYTHONPATH": "${env:PYTHONPATH};${workspaceFolder}/src;${workspaceFolder}/test"
},
"python.testing.pytestArgs": [
"test"
],
"python.testing.unittestEnabled": false,
"python.testing.pytestEnabled": true
}
}
2 changes: 1 addition & 1 deletion pytest.ini
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
[pytest]
pythonpath = src
pythonpath = src test
Empty file removed src/epg/__init__.py
Empty file.
26 changes: 13 additions & 13 deletions src/epg/authenticator.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@
credential = Credential(...)
authenticator = Authenticator(
credential = credential,
auth_method = AuthMehod.SALTED_MD5,
auth_method = AuthMethod.SALTED_MD5,
salt = "01234567"
)
encrypted_info = authenticator.info
Attributes:
AuthMehod (Enum): An enumeration of supported authentication methods.
AuthMethod (Enum): An enumeration of supported authentication methods.
CipherUtils (class): Utility class for cipher-related operations.
Authenticator (class): Handles authentication processes.
Expand All @@ -34,7 +34,7 @@
from epg.credential import Credential


class AuthMehod(Enum):
class AuthMethod(Enum):
"""Enumeration for different authentication methods.
This enumeration defines various methods that can be used for
Expand Down Expand Up @@ -130,7 +130,7 @@ class Authenticator:
Attributes:
_credential (Credential): The credential object containing user
credentials.
auth_method (AuthMehod): The method to use for authentication.
auth_method (AuthMethod): The method to use for authentication.
salt (str): The salt to use for SALTED_MD5 authentication, if
applicable.
Expand All @@ -143,7 +143,7 @@ class Authenticator:

# properties
_credential: Credential
auth_method: AuthMehod
auth_method: AuthMethod
salt: str

# properties @getter/@setter
Expand All @@ -152,7 +152,7 @@ class Authenticator:
def __init__(
self,
credential: Credential,
auth_method: AuthMehod = AuthMehod.PLAIN,
auth_method: AuthMethod = AuthMethod.PLAIN,
salt: str | None = None,
):
"""Initializes the Authenticator with given credentials and method.
Expand All @@ -164,18 +164,18 @@ def __init__(
Args:
credential (Credential): The credential object containing user
credentials.
auth_method (AuthMehod, optional): The method to use for
authentication. Defaults to AuthMehod.PLAIN.
auth_method (AuthMethod, optional): The method to use for
authentication. Defaults to AuthMethod.PLAIN.
salt (str, optional): The salt to use for SALTED_MD5
authentication. Required if auth_method is
AuthMehod.SALTED_MD5. Defaults to None.
AuthMethod.SALTED_MD5. Defaults to None.
Raises:
TypeError: If 'salt' is required for SALTED_MD5 but not provided.
"""
self._credential = credential
self.auth_method = auth_method
if auth_method == AuthMehod.SALTED_MD5:
if auth_method == AuthMethod.SALTED_MD5:
if salt is not None:
self.salt = salt
else:
Expand Down Expand Up @@ -212,11 +212,11 @@ def info(self) -> str:
credential = self._credential

match self.auth_method:
case AuthMehod.PLAIN:
case AuthMethod.PLAIN:
key = CipherUtils.pad(credential.password)
case AuthMehod.MD5:
case AuthMethod.MD5:
key = CipherUtils.md5(credential.password)
case AuthMehod.SALTED_MD5:
case AuthMethod.SALTED_MD5:
key = CipherUtils.salted_md5(credential.password, self.salt)
case _:
raise ValueError("Invalid auth key")
Expand Down
34 changes: 18 additions & 16 deletions src/epg/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
password = 'password'
cipertext = 'encrypted_string_here'
decrypted_data = reverse(password, cipertext, AuthMehod.MD5)
decrypted_data = reverse(password, cipertext, AuthMethod.MD5)
Testing salts using the EvaluateTools class:
Expand All @@ -26,17 +26,18 @@
None
"""

from multiprocessing import Manager, Process

from epg import crypto
from epg.authenticator import Authenticator, AuthMehod, CipherUtils
from epg.authenticator import Authenticator, AuthMethod, CipherUtils
from epg.credential import Credential


def reverse(
password: str,
cipertext: str,
auth_method: AuthMehod = AuthMehod.PLAIN,
auth_method: AuthMethod = AuthMethod.PLAIN,
salt: str | None = None,
) -> Credential:
"""
Expand All @@ -48,8 +49,8 @@ def reverse(
password (str): The password used for generating the key to decrypt the
ciphertext.
cipertext (str): The encrypted string that needs to be decrypted.
auth_method (AuthMehod, optional): The method of authentication to use
for decrypting the ciphertext. Defaults to AuthMehod.PLAIN.
auth_method (AuthMethod, optional): The method of authentication to use
for decrypting the ciphertext. Defaults to AuthMethod.PLAIN.
salt (str | None, optional): An optional salt that is required if the
authentication method is salted. Defaults to None.
Expand All @@ -61,14 +62,14 @@ def reverse(
Returns:
Credential: The Credential object generated from the decrypted data.
"""
if auth_method == AuthMehod.SALTED_MD5 and salt is None:
if auth_method == AuthMethod.SALTED_MD5 and salt is None:
raise TypeError("missing required positional argument: 'salt'")
match auth_method:
case AuthMehod.PLAIN:
case AuthMethod.PLAIN:
key = CipherUtils.pad(password)
case AuthMehod.MD5:
case AuthMethod.MD5:
key = CipherUtils.md5(password)
case AuthMehod.SALTED_MD5:
case AuthMethod.SALTED_MD5:
key = CipherUtils.salted_md5(password, salt)
case _:
raise ValueError("Invalid auth method")
Expand All @@ -93,6 +94,7 @@ class but rather as a scope for parallel processing of salt testing. This
The class does not return any objects but serves as a namespace to organize
related cryptographic operations effectively.
"""

_credential: Credential
_cipertext: str
_max_digitals: int
Expand Down Expand Up @@ -147,12 +149,12 @@ def _worker(self, result: list, start: int, amount: int) -> None:
if (
Authenticator(
self._credential,
AuthMehod.SALTED_MD5,
AuthMethod.SALTED_MD5,
self._pad(location + start),
).info
== self._cipertext
):
result.append(location + start)
result.append(self._pad(location + start))
except ValueError:
pass

Expand Down Expand Up @@ -185,13 +187,13 @@ def test_salt(
Returns:
list: A list of salt values that successfully match the ciphertext.
"""
self._credential=credential
self._cipertext=cipertext
self._max_digitals=max_digitals
self._padding=padding
self._credential = credential
self._cipertext = cipertext
self._max_digitals = max_digitals
self._padding = padding
total = 10**max_digitals
divided_batch = total // processes
remaining=total%processes
remaining = total % processes
with Manager() as manager:
result = manager.list()
tasks = [
Expand Down
2 changes: 2 additions & 0 deletions test/config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# pylint: disable=missing-module-docstring
TEST_DATA = "test_data.json"
4 changes: 1 addition & 3 deletions test/epg/test_crypto.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# pylint: disable=missing-module-docstring, missing-function-docstring, redefined-outer-name
# pylint: disable=missing-module-docstring, missing-class-docstring, missing-function-docstring, redefined-outer-name

import pytest

Expand Down Expand Up @@ -66,8 +66,6 @@ def test_invalid_key_length(test_data):
)




def test_encrypt_decrypt(test_data):
encrypted_data_utf8_key_8 = encrypt(
test_data["utf8_data"], test_data["key_8"]
Expand Down
51 changes: 51 additions & 0 deletions test/epg/test_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# pylint: disable=missing-module-docstring, missing-class-docstring, missing-function-docstring
import json
import random

import pytest
import random_info
from config import TEST_DATA
from epg.authenticator import Authenticator, AuthMethod
from epg.credential import Credential
from epg.utils import EvaluateTools, reverse


def test_reverse():
with pytest.raises(TypeError) as excinfo:
reverse("", "", AuthMethod.SALTED_MD5)
assert "missing required positional argument: 'salt'" in str(excinfo.value)
with pytest.raises(ValueError) as excinfo:
reverse("", "", -1)
assert "Invalid auth method" in str(excinfo.value)
with open(TEST_DATA, "r", encoding="utf-8") as file:
for authenticator_set in json.loads(file.read()):
_credential = authenticator_set["credential"]
credential = Credential(
_credential["token"],
_credential["user_id"],
_credential["password"],
_credential["ip"],
_credential["mac"],
_credential["product_id"],
_credential["ctc"],
)
print(AuthMethod[authenticator_set["method"]])
print(authenticator_set["salt"])
assert Credential.dumps(
reverse(
_credential["password"],
authenticator_set["info"],
AuthMethod[authenticator_set["method"]],
authenticator_set["salt"],
)
) == Credential.dumps(credential)


def test_EvaluateTools():
credential = random_info.credential()
salt = f"{random.randint(1,9999):05d}"
assert salt in EvaluateTools().test_salt(
credential,
Authenticator(credential, AuthMethod.SALTED_MD5, salt).info,
5,
)
3 changes: 2 additions & 1 deletion test/generate_data.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# pylint: disable=missing-module-docstring, missing-class-docstring, missing-function-docstring
from config import TEST_DATA
from test_data import batch_generate

with open("test_data.json", "w", encoding="utf-8") as file:
with open(TEST_DATA, "w", encoding="utf-8") as file:
file.write(batch_generate())
13 changes: 7 additions & 6 deletions test/random_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import random
import string

from epg.authenticator import Authenticator, AuthMehod
from epg.authenticator import Authenticator, AuthMethod
from epg.credential import Credential

BASIC_SET = (
Expand Down Expand Up @@ -79,9 +79,9 @@ def batch_credential(amount: int) -> list:


def authenticator() -> dict[Authenticator, str]:
method = random.choice(list(AuthMehod))
method = random.choice(list(AuthMethod))
_credential = credential()
if method == AuthMehod.SALTED_MD5:
if method == AuthMethod.SALTED_MD5:
salt = f"{random.randint(0,99999999):08d}"
else:
salt = None
Expand All @@ -94,19 +94,20 @@ def batch_authenticator(amount: int) -> list[dict[Authenticator, str]]:


def raw_authenticator() -> dict[str]:
method = random.choice(list(AuthMehod))
method = random.choice(list(AuthMethod))
_credential = credential()
if method == AuthMehod.SALTED_MD5:
if method == AuthMethod.SALTED_MD5:
salt = f"{random.randint(0,99999999):08d}"
else:
salt = None
_authenticator = Authenticator(_credential, method, salt)
return {
"credential": dict(credential()),
"credential": dict(_credential),
"method": method.name,
"salt": salt,
"info": _authenticator.info,
}


def batch_raw_authenticator(amount: int) -> list[dict[Authenticator, str]]:
return [raw_authenticator() for _ in range(amount)]
3 changes: 2 additions & 1 deletion test/test_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import json
import random

from config import TEST_DATA
from random_info import batch_raw_authenticator


Expand All @@ -13,5 +14,5 @@ def batch_generate():


def test_data():
with open("test_data.json", "r", encoding="utf-8") as file:
with open(TEST_DATA, "r", encoding="utf-8") as file:
assert file.read() == batch_generate()
Loading

0 comments on commit e983466

Please sign in to comment.