Commit | Line | Data |
---|---|---|
fb2a6083 CH |
1 | #!/usr/bin/env python3 |
2 | ||
3 | """ Encrypt a single file | |
4 | ||
5 | low-cost quick-devel badly-documented | |
6 | ||
7 | .. codeauthor:: Intra2net <info@intra2net.com> | |
8 | """ | |
9 | ||
10 | import sys | |
11 | from deltatar import tarfile | |
12 | from traceback import print_exc | |
13 | ||
14 | ||
15 | def main(do_encrypt, in_file, out_file, password, comptype='gz', enctype='aes', | |
e37669e3 | 16 | bufsize=tarfile.RECORDSIZE, encoding='UTF-8'): |
fb2a6083 CH |
17 | """ Main function, called when running file as script |
18 | ||
19 | see module doc for more info | |
20 | """ | |
21 | ||
22 | write_handle = read_handle = None | |
23 | return_code = 4 | |
24 | ||
25 | try: | |
26 | # open file to read | |
27 | if do_encrypt: | |
28 | read_handle = open(in_file, 'rt') | |
29 | else: | |
30 | read_handle = tarfile._Stream(name=in_file, mode='r', | |
31 | comptype=comptype, bufsize=bufsize, | |
32 | fileobj=None, enctype=enctype, | |
fb2a6083 CH |
33 | password=password) |
34 | return_code = 3 | |
35 | ||
36 | # open file to write | |
37 | if do_encrypt: | |
38 | write_handle = tarfile._Stream(name=out_file, mode='w', | |
39 | comptype=comptype, bufsize=bufsize, | |
40 | fileobj=None, enctype=enctype, | |
fb2a6083 CH |
41 | password=password) |
42 | else: | |
43 | write_handle = open(out_file, 'wt') | |
44 | return_code = 1 | |
45 | ||
46 | # convert | |
47 | while True: | |
48 | buf = read_handle.read(bufsize) | |
49 | print('.', end='') | |
50 | if do_encrypt: | |
51 | write_handle.write(buf.encode(encoding, errors='strict')) | |
52 | else: | |
53 | write_handle.write(buf.decode(encoding, errors='replace')) | |
54 | if len(buf) < bufsize: | |
55 | if do_encrypt: | |
56 | print('successfully encrypted {} into {}' | |
57 | .format(in_file, out_file)) | |
58 | else: | |
59 | print('successfully decrypted {} into {}' | |
60 | .format(in_file, out_file)) | |
61 | break # reached EOF | |
62 | ||
63 | return_code = 0 | |
64 | ||
65 | except Exception: | |
66 | print('error encrypting file') | |
67 | print_exc() | |
68 | ||
69 | finally: | |
70 | # close everything | |
71 | if write_handle: | |
72 | try: | |
73 | write_handle.close() | |
74 | except Exception: | |
75 | return_code += 8 | |
76 | print('error closing out file') | |
77 | print_exc() | |
78 | ||
79 | if read_handle: | |
80 | try: | |
81 | read_handle.close() | |
82 | except Exception: | |
83 | return_code += 16 | |
84 | print('error closing in file') | |
85 | print_exc() | |
86 | ||
87 | # done | |
88 | return return_code | |
89 | ||
90 | ||
91 | if __name__ == '__main__': | |
92 | if len(sys.argv) < 4: | |
93 | print('file_crypt.py {enc|dec} [-c] INFILE OUTFILE PASSWORD') | |
94 | sys.exit(2) | |
95 | elif sys.argv[1] == '-h': | |
96 | print('encrypt_file.py {enc|dec} [-c] INFILE OUTFILE PASSWORD') | |
97 | sys.exit(0) | |
98 | ||
99 | comptype = 'tar' # auto-detect, not sure whether this would work | |
100 | idx = 1 | |
101 | if sys.argv[idx] == '-c': | |
102 | print('assuming gzip compression') | |
103 | comptype = 'gz' | |
104 | idx += 1 | |
105 | ||
106 | do_encrypt = None | |
107 | if sys.argv[idx] == 'enc': | |
108 | do_encrypt = True | |
109 | elif sys.argv[idx] == 'dec': | |
110 | do_encrypt = False | |
111 | else: | |
112 | print('encrypt_file.py {enc|dec} [-c] INFILE OUTFILE PASSWORD') | |
113 | sys.exit(2) | |
114 | ||
115 | if len(sys.argv) != idx+4: | |
116 | print('encrypt_file.py {enc|dec} [-c] INFILE OUTFILE PASSWORD') | |
117 | sys.exit(2) | |
118 | ||
119 | sys.exit(main(do_encrypt, sys.argv[idx+1], sys.argv[idx+2], | |
120 | sys.argv[idx+3], comptype)) |