-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
68 lines (49 loc) · 2.53 KB
/
main.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
import argparse
from fileutils import *
def test():
ensure_rsa_keys_exists()
TEST_FILE = "data/filetoencrypt.txt"
TEST_JSON = TEST_FILE + ".json"
# Reads the test file, encrypts (but doesn't delete) it.
encrypt_file(TEST_FILE, TEST_JSON)
# Reads the encrypted json file, decrypts (but doesn't delete) it.
decrypt_file(TEST_JSON, TEST_FILE + "_output.txt")
# For now, just remove one of these to see it work. I'll eventually add command line args to select
encrypt_all_files(constants.ROOT_FOLDER)
decrypt_all_files(constants.ROOT_FOLDER)
def ui():
parser = argparse.ArgumentParser(description='TRAD\'s File Encryption.') # TODO: Finish the description
group = parser.add_mutually_exclusive_group(required=True)
group.add_argument('-e', '--encrypt', action="store_true",
help='Encrypt the specified file(s)')
group.add_argument('-d', '--decrypt', action="store_true",
help='Decrypt the specified files')
parser.add_argument('path', type=lambda x: verify_path(parser, x), default=constants.ROOT_FOLDER,
help='either a single file or the root directory to traverse (default: "' + constants.ROOT_FOLDER + '")')
parser.add_argument('--key-private', default=constants.RSA_PRIVATEKEY_FILEPATH,
help='Path of private RSA key. If one doesn\'t exist here it will be created. (default: "' + constants.RSA_PRIVATEKEY_FILEPATH + '")')
parser.add_argument('--key-public', default=constants.RSA_PUBLICKEY_FILEPATH,
help='Path of public RSA key. If one doesn\'t exist here it will be created. (default: "' + constants.RSA_PUBLICKEY_FILEPATH + '")')
args = parser.parse_args()
# Set up arguments
rootdir = args.path
constants.RSA_PRIVATEKEY_FILEPATH = args.key_private # TODO I shouldn't be changing constants. Need to rework methods to include an optional parameter
constants.RSA_PUBLICKEY_FILEPATH = args.key_public
# Perform encryption/decryption
ensure_rsa_keys_exists()
if path.isdir(rootdir):
if args.encrypt:
encrypt_all_files(rootdir=rootdir)
if args.decrypt:
decrypt_all_files(rootdir=rootdir)
else: # TODO: Finish single file encryption/decryption
if args.encrypt:
encrypt_file()
if args.decrypt:
decrypt_file()
def verify_path(parser, arg):
if not path.exists(arg):
parser.error("The path %s does not exist" % arg)
else:
return path.abspath(arg)
ui()