-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add option to pseudonymizer to use different encryption modes (#600)
* add mode option to pseudonymizer * move encrypter to pseudo module * update changelog * update documentation * add test for pseudo tools workflow with different modes * implement __str__ methods in Pseudonyms --------- Co-authored-by: dtrai2 <[email protected]>
- Loading branch information
Showing
19 changed files
with
849 additions
and
460 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
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
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,21 +1,41 @@ | ||
"""Command line tool to depseudonymize a string using the given keys.""" | ||
|
||
import logging | ||
import sys | ||
|
||
import click | ||
|
||
from logprep.util.pseudo.depseudonymizer.depseudonymizer import Depseudonymizer | ||
from logprep.util.pseudo.decrypter import ( | ||
DualPKCS1HybridCTRDecrypter, | ||
DualPKCS1HybridGCMDecrypter, | ||
) | ||
|
||
|
||
@click.command() | ||
@click.argument("analyst-key", type=str) | ||
@click.argument("depseudo-key", type=str) | ||
@click.argument("analyst-key", type=click.Path(exists=True)) | ||
@click.argument("depseudo-key", type=click.Path(exists=True)) | ||
@click.argument("pseudo-string", type=str) | ||
def depseudonymize(analyst_key: str, depseudo_key: str, pseudo_string: str): | ||
@click.option( | ||
"--mode", | ||
type=click.Choice(["gcm", "ctr"]), | ||
default="ctr", | ||
help="The mode to use for decryption", | ||
) | ||
def depseudonymize(analyst_key: str, depseudo_key: str, pseudo_string: str, mode: str): | ||
"""depseudonymize a string using the given keys.""" | ||
depseudo = Depseudonymizer(pseudo_string) | ||
depseudo = ( | ||
DualPKCS1HybridGCMDecrypter(pseudo_string) | ||
if mode == "gcm" | ||
else DualPKCS1HybridCTRDecrypter(pseudo_string) | ||
) | ||
keys = {} | ||
for key_file_name in analyst_key, depseudo_key: | ||
with open(f"{key_file_name}.key", "r", encoding="utf8") as key_file: | ||
with open(f"{key_file_name}", "r", encoding="utf8") as key_file: | ||
keys[key_file_name] = key_file.read() | ||
depseudo.depseudo_key = keys[depseudo_key] | ||
depseudo.analyst_key = keys[analyst_key] | ||
print(depseudo.depseudonymize()) | ||
try: | ||
print(depseudo.decrypt()) | ||
except Exception as e: # pylint: disable=broad-except | ||
print(f"Error: {e}", file=sys.stderr) | ||
sys.exit(1) |
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,17 +1,28 @@ | ||
"""Pseudonymize a string using the given keys and method.""" | ||
|
||
import click | ||
|
||
from logprep.processor.pseudonymizer.encrypter import DualPKCS1HybridEncrypter | ||
from logprep.util.pseudo.encrypter import ( | ||
DualPKCS1HybridCTREncrypter, | ||
DualPKCS1HybridGCMEncrypter, | ||
) | ||
|
||
|
||
@click.command() | ||
@click.argument("analyst-key", type=str) | ||
@click.argument("depseudo-key", type=str) | ||
@click.argument("analyst-key", type=click.Path(exists=True)) | ||
@click.argument("depseudo-key", type=click.Path(exists=True)) | ||
@click.argument("string", type=str) | ||
def pseudonymize(analyst_key: str, depseudo_key: str, string: str): | ||
@click.option( | ||
"--mode", | ||
type=click.Choice(["gcm", "ctr"]), | ||
default="ctr", | ||
help="The mode to use for decryption", | ||
) | ||
def pseudonymize(analyst_key: str, depseudo_key: str, string: str, mode: str): | ||
"""pseudonymize a string using the given keys.""" | ||
encrypter = DualPKCS1HybridEncrypter() | ||
encrypter = DualPKCS1HybridGCMEncrypter() if mode == "gcm" else DualPKCS1HybridCTREncrypter() | ||
encrypter.load_public_keys( | ||
keyfile_analyst=f"{analyst_key}.crt", | ||
keyfile_depseudo=f"{depseudo_key}.crt", | ||
keyfile_analyst=f"{analyst_key}", | ||
keyfile_depseudo=f"{depseudo_key}", | ||
) | ||
print(encrypter.encrypt(string)) |
Oops, something went wrong.