-
Notifications
You must be signed in to change notification settings - Fork 0
/
crypto.py
109 lines (89 loc) · 2.51 KB
/
crypto.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
import click
from typing import Union, Optional
from src.encryption.decrypt import Decryptor
from src.encryption.encrypt import Encryptor
@click.group()
@click.option(
"--output_folder",
"-o",
help="Output folder for encrypted/decrypted items.",
default="./",
)
@click.option(
"--iterations",
"-i",
help="Amount of iterations of the hashing algorithm.",
default=int(1e5),
)
@click.option(
"--salt", "-s", help="Custom value for the hashing salt.", default=None
)
@click.pass_context
def main(
ctx,
output_folder: str,
iterations: Union[int, float],
salt: Optional[Union[int, float, str]] = None,
):
"""
Simple tool to encrypt or decrypt your files and folders using the SHA-2 algorithm with 512-bit hashing.
"""
# Add properties to context object
ctx.obj["output_folder"] = output_folder
ctx.obj["iterations"] = iterations
ctx.obj["salt"] = salt
@main.command()
@click.option(
"--save_key",
"-S",
help="Save the encryption key to disk.",
default=False,
is_flag=True,
)
@click.argument("targets", nargs=-1)
@click.pass_context
def encrypt(ctx, save_key: bool = False, **kwargs):
"""
Encrypt file(s), or folder(s).
"""
# Class object instance
encryptor = Encryptor()
# Add property to context object
ctx.obj["save_key"] = save_key
# Handlers for direct and indirect encryption targets
encrypt_params = {
"output_folder": ctx.obj["output_folder"],
"iterations": ctx.obj["iterations"],
"salt": ctx.obj["salt"],
"save_key": ctx.obj["save_key"],
}
if len(kwargs["targets"]) > 0:
encrypt_params.update(targets=kwargs["targets"])
encryptor.encrypt_targets(**encrypt_params)
else:
encryptor.encrypt_targets(**encrypt_params)
@main.command()
@click.argument("targets", nargs=-1)
@click.pass_context
def decrypt(ctx, **kwargs):
"""
Decrypt file(s), or folder(s).
"""
# Class object instance
decryptor = Decryptor()
# Handlers for direct and indirect encryption targets
decrypt_params = {
"output_folder": ctx.obj["output_folder"],
"iterations": ctx.obj["iterations"],
"salt": ctx.obj["salt"],
}
if len(kwargs["targets"]) > 0:
decrypt_params.update(targets=kwargs["targets"])
decryptor.decrypt_targets(**decrypt_params)
else:
decryptor.decrypt_targets(**decrypt_params)
# Entrypoint and context object
def start():
main(obj={})
if __name__ == "__main__":
start()