-
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.
feat: open for all hashes from
hashlib
- Loading branch information
Showing
12 changed files
with
170 additions
and
187 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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,23 +1,47 @@ | ||
import secrets | ||
|
||
from ... import bytestr_utils as bu | ||
from ...encryption_algorithms.simple import common | ||
from ... import bytestr_utils as bsu | ||
from ... import bytes_utils as bu | ||
from ... import puzzle_data_encryption as pde | ||
from ... import run_puzzle as rp | ||
|
||
MODULES = ["hashlib", "base64", "sys", "typing"] | ||
|
||
OBJECTS = [ | ||
common.hash_bytes, | ||
common.derive_key, | ||
common.split_data_and_signature, | ||
common.digest_size, | ||
common.xor_bytes, | ||
bsu.bytestr_to_bytes, | ||
bu.bytes_to_int, | ||
bu.split, | ||
pde.decrypt_data, | ||
rp.run_puzzle, | ||
] | ||
|
||
|
||
def scrypt_params(**kwargs): | ||
print(kwargs) | ||
in_params = kwargs.get("scrypt_params", {}) | ||
default = {"salt": secrets.token_bytes(16), "n": 2**14, "r": 8, "p": 1} | ||
if "maxmem" in kwargs: | ||
default["maxmem"] = kwargs["maxmem"] | ||
res = {_k: kwargs.get(_k, _v) for _k, _v in default.items()} | ||
if "maxmem" in in_params: | ||
default["maxmem"] = in_params["maxmem"] | ||
res = {_k: in_params.get(_k, _v) for _k, _v in default.items()} | ||
if "maxmem" not in res: | ||
res["maxmem"] = (128 + 100) * res["n"] * res["r"] * res["p"] | ||
return res | ||
|
||
|
||
def signature_params(**kwargs): | ||
res = kwargs.get("signature_params", {"hasher": {"name": "sha512"}, "digest": {}}) | ||
if "digest" not in res: | ||
res["digest"] = {} | ||
return res | ||
|
||
|
||
def scrypt_params_to_code_str(**kwargs) -> str: | ||
pieces = [f'"{_k}":{_v}' for _k, _v in kwargs.items() if _k != "salt"] | ||
salt_str = '"' + bu.bytes_to_bytestr(kwargs["salt"]) + '"' | ||
salt_str = '"' + bsu.bytes_to_bytestr(kwargs["salt"]) + '"' | ||
pieces.append(f'"salt":bytestr_to_bytes({salt_str})') | ||
return f"_SCRYPT_PARAMS = {{{','.join(pieces)}}}" |
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,29 @@ | ||
import typing | ||
|
||
from ... import bytestr_utils as bu | ||
from ...encryption_algorithms.simple import common | ||
from ...encryption_algorithms.simple import simple as se | ||
from ...puzzle_data_encryption import decrypt_data | ||
from .. import common as cc | ||
from .common import MODULES, scrypt_params, scrypt_params_to_code_str | ||
from ...run_puzzle import run_puzzle | ||
from ...bytes_utils import bytes_to_int, split | ||
from . import common as csc | ||
|
||
|
||
class Simple: | ||
def __init__(self, **kwargs): | ||
self._scrypt_params = scrypt_params(**kwargs) | ||
self._signature_hasher = kwargs.get("signature_hasher", cc.DefaultHasher) | ||
self._scrypt_params = csc.scrypt_params(**kwargs) | ||
self._signature_params = csc.signature_params(**kwargs) | ||
|
||
def get_modules(self) -> typing.List[str]: | ||
return MODULES | ||
return csc.MODULES | ||
|
||
def get_encrypt(self): | ||
return se.get_encrypt(self._signature_hasher, self._scrypt_params) | ||
return se.get_encrypt(self._scrypt_params, self._signature_params) | ||
|
||
def get_needed_objects(self): | ||
return [ | ||
common.hash_bytes, | ||
common.split_data_and_signature, | ||
common.derive_key, | ||
common.xor_bytes, | ||
bu.bytestr_to_bytes, | ||
return csc.OBJECTS + [ | ||
se.get_decrypt, | ||
bytes_to_int, | ||
split, | ||
decrypt_data, | ||
run_puzzle, | ||
] | ||
|
||
def get_constants_str( | ||
self, | ||
) -> str: | ||
_scrypt_params = scrypt_params_to_code_str(**self._scrypt_params) | ||
decrypt: str = ( | ||
"_DECRYPT = get_decrypt(" | ||
f"{cc.get_hasher_name(self._signature_hasher)}, " | ||
"_SCRYPT_PARAMS)" | ||
) | ||
return "\n".join([_scrypt_params, decrypt]) | ||
_scrypt_params = csc.scrypt_params_to_code_str(**self._scrypt_params) | ||
_signature_params = "_SIGNATURE_PARAMS = " + repr(self._signature_params) | ||
decrypt: str = "_DECRYPT = get_decrypt(_SCRYPT_PARAMS, _SIGNATURE_PARAMS)" | ||
return "\n".join([_scrypt_params, _signature_params, 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
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
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 |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import pytest | ||
|
||
import puzzle_generator.encryption_algorithms.simple.common as eac | ||
from .. import utils | ||
|
||
|
||
@pytest.mark.parametrize("in_hash_params", utils.SOME_SIGNATURE_PARAMS) | ||
def test_digest_size(in_hash_params): | ||
some_hash = eac.hash_bytes(b"some_msg", in_hash_params) | ||
assert eac.digest_size(in_hash_params) == len(some_hash) |
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
Oops, something went wrong.