Skip to content

Commit

Permalink
refactor: create simple_encryption
Browse files Browse the repository at this point in the history
  • Loading branch information
vil02 committed Jul 26, 2024
1 parent 386cb86 commit 5fcce2e
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 44 deletions.
5 changes: 3 additions & 2 deletions puzzle_generator/create_puzzle.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

from .puzzle_data_encryption import decrypt_data, encrypt_data
from . import simple_encryption_utils as seu
from . import simple_encryption as se
from . import bytestr_utils as bu


Expand Down Expand Up @@ -56,7 +57,7 @@ def create(in_puzzle, output_path: pathlib.Path, **kwargs) -> None:
proc_hasher = kwargs.get("proc_hasher", hashlib.sha512)
signature_hasher = kwargs.get("signature_hasher", hashlib.sha512)
encrypted_puzzle = encrypt_data(
in_puzzle, seu.get_encrypt(proc_hasher, signature_hasher)
in_puzzle, se.get_encrypt(proc_hasher, signature_hasher)
)

needed_modules = ["hashlib", "itertools", "base64", "json", "sys", "typing"]
Expand All @@ -66,7 +67,7 @@ def create(in_puzzle, output_path: pathlib.Path, **kwargs) -> None:
seu.int_to_bytes,
seu.proc_bytes,
bu.bytestr_to_bytes,
seu.get_decrypt,
se.get_decrypt,
decrypt_data,
_run_puzzle,
]
Expand Down
38 changes: 38 additions & 0 deletions puzzle_generator/simple_encryption.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import typing

from .simple_encryption_utils import proc_bytes, hash_bytes
from .bytestr_utils import bytes_to_bytestr, bytestr_to_bytes


def get_encrypt(
proc_hasher, signature_hasher
) -> typing.Callable[[str, str], typing.Tuple[str, str]]:
def _encrypt_bytes(in_bytes: bytes, in_pass: bytes) -> typing.Tuple[bytes, str]:
return proc_bytes(in_bytes, in_pass, proc_hasher), hash_bytes(
in_bytes, signature_hasher
)

def _encrypt(in_str: str, in_pass: str) -> typing.Tuple[str, str]:
encrypted_bytes, hash_str = _encrypt_bytes(in_str.encode(), in_pass.encode())
return bytes_to_bytestr(encrypted_bytes), hash_str

return _encrypt


def get_decrypt(
proc_hasher, signature_hasher
) -> typing.Callable[[str, str, str], str | None]:
def _decrypt_bytes(in_bytes: bytes, in_pass: bytes, in_hash: str) -> bytes | None:
res = proc_bytes(in_bytes, in_pass, proc_hasher)

if hash_bytes(res, signature_hasher) == in_hash:
return res
return None

def _decrypt(in_str: str, in_pass: str, in_hash: str) -> str | None:
res = _decrypt_bytes(bytestr_to_bytes(in_str), in_pass.encode(), in_hash)
if res is not None:
return res.decode()
return res

return _decrypt
37 changes: 0 additions & 37 deletions puzzle_generator/simple_encryption_utils.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
import itertools
import typing

from .bytestr_utils import bytes_to_bytestr, bytestr_to_bytes


def hash_bytes(in_bytes: bytes, in_hasher) -> str:
Expand All @@ -19,37 +16,3 @@ def proc_bytes(in_bytes: bytes, in_key: bytes, in_hasher) -> bytes:
for block_num in itertools.count(0)
)
return bytes(_d ^ _k for (_d, _k) in zip(in_bytes, key_bytes))


def get_encrypt(
proc_hasher, signature_hasher
) -> typing.Callable[[str, str], typing.Tuple[str, str]]:
def _encrypt_bytes(in_bytes: bytes, in_pass: bytes) -> typing.Tuple[bytes, str]:
return proc_bytes(in_bytes, in_pass, proc_hasher), hash_bytes(
in_bytes, signature_hasher
)

def _encrypt(in_str: str, in_pass: str) -> typing.Tuple[str, str]:
encrypted_bytes, hash_str = _encrypt_bytes(in_str.encode(), in_pass.encode())
return bytes_to_bytestr(encrypted_bytes), hash_str

return _encrypt


def get_decrypt(
proc_hasher, signature_hasher
) -> typing.Callable[[str, str, str], str | None]:
def _decrypt_bytes(in_bytes: bytes, in_pass: bytes, in_hash: str) -> bytes | None:
res = proc_bytes(in_bytes, in_pass, proc_hasher)

if hash_bytes(res, signature_hasher) == in_hash:
return res
return None

def _decrypt(in_str: str, in_pass: str, in_hash: str) -> str | None:
res = _decrypt_bytes(bytestr_to_bytes(in_str), in_pass.encode(), in_hash)
if res is not None:
return res.decode()
return res

return _decrypt
6 changes: 3 additions & 3 deletions tests/test_encryption_utils.py → tests/test_encryptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import itertools
import pytest

import puzzle_generator.simple_encryption_utils as seu
import puzzle_generator.simple_encryption as se


_STRS = [
Expand All @@ -26,7 +26,7 @@


def _get_encrypt_decrypt_pair(proc_hasher, signature_hasher):
return seu.get_encrypt(proc_hasher, signature_hasher), seu.get_decrypt(
return se.get_encrypt(proc_hasher, signature_hasher), se.get_decrypt(
proc_hasher, signature_hasher
)

Expand All @@ -37,7 +37,7 @@ def _get_encrypt_decrypt_pair(proc_hasher, signature_hasher):
("encrypt", "decrypt"),
[_get_encrypt_decrypt_pair(*_) for _ in itertools.product(_SOME_HASHES, repeat=2)],
)
def test_seu(in_str, in_pass, encrypt, decrypt):
def test_encryption_decryption(in_str, in_pass, encrypt, decrypt):
encrypted, reshash = encrypt(in_str, in_pass)
if in_str:
assert encrypted != in_str
Expand Down
4 changes: 2 additions & 2 deletions tests/test_puzzle_data_encryption.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import pytest

import puzzle_generator.puzzle_data_encryption as pde
import puzzle_generator.simple_encryption_utils as seu
import puzzle_generator.simple_encryption as se

_SOME_HASHES = [
hashlib.sha1,
Expand All @@ -12,7 +12,7 @@


def _get_encrypt_decrypt_pair(proc_hasher, signature_hasher):
return seu.get_encrypt(proc_hasher, signature_hasher), seu.get_decrypt(
return se.get_encrypt(proc_hasher, signature_hasher), se.get_decrypt(
proc_hasher, signature_hasher
)

Expand Down

0 comments on commit 5fcce2e

Please sign in to comment.