-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: open for different encryption methods
- Loading branch information
Showing
5 changed files
with
129 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,47 +1,55 @@ | ||
import itertools | ||
import typing | ||
import hashlib | ||
|
||
from .bytestr_utils import bytes_to_bytestr, bytestr_to_bytes | ||
|
||
|
||
def hash_bytes(in_bytes: bytes) -> str: | ||
return hashlib.sha512(in_bytes).hexdigest() | ||
def hash_bytes(in_bytes: bytes, in_hasher) -> str: | ||
return str(in_hasher(in_bytes).hexdigest()) | ||
|
||
|
||
def int_to_bytes(in_val: int) -> bytes: | ||
return in_val.to_bytes((in_val.bit_length() + 7) // 8, "big") | ||
|
||
|
||
def proc_bytes(in_bytes: bytes, in_key: bytes) -> bytes: | ||
def proc_bytes(in_bytes: bytes, in_key: bytes, in_hasher) -> bytes: | ||
"""xors the in_bytes with a sequence of bytes generated with in_key""" | ||
key_bytes = itertools.chain.from_iterable( | ||
hashlib.sha512(in_key + int_to_bytes(block_num)).digest() | ||
in_hasher(in_key + int_to_bytes(block_num)).digest() | ||
for block_num in itertools.count(0) | ||
) | ||
return bytes(_d ^ _k for (_d, _k) in zip(in_bytes, key_bytes)) | ||
|
||
|
||
def encrypt_bytes(in_bytes: bytes, in_pass: bytes) -> typing.Tuple[bytes, str]: | ||
return proc_bytes(in_bytes, in_pass), hash_bytes(in_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 | ||
|
||
def decrypt_bytes(in_bytes: bytes, in_pass: bytes, in_hash: str) -> bytes | None: | ||
res = proc_bytes(in_bytes, in_pass) | ||
return _encrypt | ||
|
||
if hash_bytes(res) == in_hash: | ||
return res | ||
return None | ||
|
||
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) | ||
|
||
def encrypt_str(in_str: str, in_pass: str) -> typing.Tuple[str, str]: | ||
"""encrypts in_str using in_pass""" | ||
encrypted_bytes, hash_str = encrypt_bytes(in_str.encode(), in_pass.encode()) | ||
return bytes_to_bytestr(encrypted_bytes), hash_str | ||
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 | ||
|
||
def decrypt_str(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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,49 @@ | ||
import string | ||
import hashlib | ||
import itertools | ||
import pytest | ||
|
||
import puzzle_generator.simple_encryption_utils as seu | ||
|
||
|
||
_STRS = [ | ||
"", | ||
"some_str", | ||
"other_strstr!?#", | ||
"some_STR?!", | ||
string.printable, | ||
string.ascii_uppercase, | ||
string.whitespace, | ||
"ąęćśłń󿟥ĘĆŚŁŃÓŻŹ", | ||
"some_msg_with 🔨 and 🛷!", | ||
"a🎄b", | ||
"🎮🎈🥅🐾", | ||
"🎮🎈🥅🐾 a🎄b", | ||
"🏀", | ||
] | ||
|
||
_SOME_HASHES = [ | ||
hashlib.md5, | ||
hashlib.sha512, | ||
hashlib.sha3_512, | ||
hashlib.blake2s, | ||
] | ||
|
||
|
||
def _get_encrypt_decrypt_pair(proc_hasher, signature_hasher): | ||
return seu.get_encrypt(proc_hasher, signature_hasher), seu.get_decrypt( | ||
proc_hasher, signature_hasher | ||
) | ||
|
||
|
||
@pytest.mark.parametrize("in_str", _STRS) | ||
@pytest.mark.parametrize("in_pass", _STRS) | ||
@pytest.mark.parametrize( | ||
("in_encrypt_str", "in_decrypt_str"), [(seu.encrypt_str, seu.decrypt_str)] | ||
("encrypt", "decrypt"), | ||
[_get_encrypt_decrypt_pair(*_) for _ in itertools.product(_SOME_HASHES, repeat=2)], | ||
) | ||
def test_seu(in_str, in_pass, in_encrypt_str, in_decrypt_str): | ||
encrypted, reshash = in_encrypt_str(in_str, in_pass) | ||
def test_seu(in_str, in_pass, encrypt, decrypt): | ||
encrypted, reshash = encrypt(in_str, in_pass) | ||
if in_str: | ||
assert encrypted != in_str | ||
else: | ||
assert not encrypted | ||
decrypted = in_decrypt_str(encrypted, in_pass, reshash) | ||
decrypted = decrypt(encrypted, in_pass, reshash) | ||
assert decrypted == in_str | ||
if in_str: | ||
assert in_decrypt_str(encrypted, in_pass + "?", reshash) is None | ||
assert decrypt(encrypted, in_pass + "?", reshash) is None |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters