Skip to content

Commit dc1e77a

Browse files
committed
Improve code quality
1 parent e1e55c7 commit dc1e77a

File tree

2 files changed

+82
-66
lines changed

2 files changed

+82
-66
lines changed

project/__main__.py

Lines changed: 18 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
from .crypt import encrypt, decrypt
2-
from .utils import write_file
3-
from .crack import bruteforce
1+
from .handlers import bruteforce_handler, decrypt_handler, encrypt_handler
42
import argparse
53

64
parser = argparse.ArgumentParser(
@@ -31,67 +29,21 @@
3129

3230

3331
def handle_args():
34-
# If key isn't in the args,
35-
if not args.key and (args.encrypt or args.decrypt):
36-
raise NameError("Key is not defined")
37-
# If the bruteforce flag used
38-
elif args.brute:
39-
c_value = ' '.join(args.brute)
40-
cracked = []
41-
if args.sourceFile:
42-
f = open(c_value, 'r').read()
43-
cracked = bruteforce(f)
32+
try:
33+
# If key isn't in the args,
34+
if not args.key and (args.encrypt or args.decrypt):
35+
raise NameError("Key is not defined")
36+
# If the bruteforce flag used
37+
elif args.bruteforce:
38+
bruteforce_handler(args)
39+
# If the encrypt flag used
40+
elif args.encrypt:
41+
encrypt_handler(args)
42+
# If decrypt flag used,
43+
elif args.decrypt:
44+
decrypt_handler(args)
45+
# If no flags used,
4446
else:
45-
cracked = bruteforce(c_value)
46-
# Write to the file,
47-
# If an output path specified
48-
if args.output:
49-
write_file(cracked, args.output)
50-
else:
51-
for j in cracked:
52-
print(j)
53-
# If the encrypt flag used
54-
elif args.encrypt:
55-
# Extract key as an int from args
56-
key = int(''.join(map(str, args.key)))
57-
key *= -1 if args.backwards else 1
58-
# Get plain text
59-
e_value = ' '.join(args.encrypt)
60-
# If source presents,
61-
# file content will be encrypted
62-
cipher = ''
63-
if args.sourceFile:
64-
f = open(e_value, 'r').read()
65-
cipher = encrypt(f, key)
66-
else:
67-
cipher = encrypt(e_value, key)
68-
# Write to the file,
69-
# If an output path specified
70-
if args.output:
71-
write_file(cipher, args.output)
72-
else:
73-
print("Encrypted: " + cipher)
74-
# If decrypt flag used,
75-
elif args.decrypt:
76-
# Extract key as an int from args
77-
key = int(''.join(map(str, args.key)))
78-
key *= -1 if args.backwards else 1
79-
# Get Cipher text
80-
d_value = ' '.join(args.decrypt)
81-
# If source presents,
82-
# file content will be decrypted
83-
plain = ''
84-
if args.sourceFile:
85-
f = open(d_value, 'r').read()
86-
plain = decrypt(f, key)
87-
else:
88-
plain = decrypt(d_value, key)
89-
# Write to the file,
90-
# If an output path specified
91-
if args.output:
92-
write_file(plain, args.output)
93-
else:
94-
print("Decrypted: " + plain)
95-
# If no flags used,
96-
else:
97-
raise ValueError("No Arguments given")
47+
raise ValueError("No Arguments given")
48+
except Exception as e:
49+
print("Error:", e)

project/handlers.py

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
from .crack import bruteforce
2+
from .utils import write_file
3+
from .crypt import encrypt, decrypt
4+
5+
6+
def bruteforce_handler(args):
7+
c_value = ' '.join(args.bruteforce)
8+
cracked = []
9+
if args.sourceFile:
10+
f = open(c_value, 'r').read()
11+
cracked = bruteforce(f)
12+
else:
13+
cracked = bruteforce(c_value)
14+
# Write to the file,
15+
# If an output path specified
16+
if args.output:
17+
write_file(cracked, args.output)
18+
else:
19+
for j in cracked:
20+
print(j)
21+
22+
23+
def encrypt_handler(args):
24+
# Extract key as an int from args
25+
key = int(''.join(map(str, args.key)))
26+
key *= -1 if args.backwards else 1
27+
# Get plain text
28+
e_value = ' '.join(args.encrypt)
29+
# If source presents,
30+
# file content will be encrypted
31+
cipher = ''
32+
if args.sourceFile:
33+
f = open(e_value, 'r').read()
34+
cipher = encrypt(f, key)
35+
else:
36+
cipher = encrypt(e_value, key)
37+
# Write to the file,
38+
# If an output path specified
39+
if args.output:
40+
write_file(cipher, args.output)
41+
else:
42+
print("Encrypted: " + cipher)
43+
44+
45+
def decrypt_handler(args):
46+
# Extract key as an int from args
47+
key = int(''.join(map(str, args.key)))
48+
key *= -1 if args.backwards else 1
49+
# Get Cipher text
50+
d_value = ' '.join(args.decrypt)
51+
# If source presents,
52+
# file content will be decrypted
53+
plain = ''
54+
if args.sourceFile:
55+
f = open(d_value, 'r').read()
56+
plain = decrypt(f, key)
57+
else:
58+
plain = decrypt(d_value, key)
59+
# Write to the file,
60+
# If an output path specified
61+
if args.output:
62+
write_file(plain, args.output)
63+
else:
64+
print("Decrypted: " + plain)

0 commit comments

Comments
 (0)