diff --git a/compress.py b/compress.py index ad12bc5..a72deca 100644 --- a/compress.py +++ b/compress.py @@ -1,6 +1,8 @@ import os import re import gzip +import tempfile +from crypt import * def recurse(path, maxsize="50M"): @@ -12,17 +14,20 @@ def recurse(path, maxsize="50M"): Hsize = str(human_readable_size(size, 0)) print(f + " : " + Hsize) if size > max: - compress(uri) + compress(uri, key) elif os.path.isdir(uri): recurse(uri) -def compress(file): +def compress(file, key): + gz = tempfile.SpooledTemporaryFile() with open(file, 'rb') as infile: - with gzip.open(file + ".gz", 'wb') as zipfile: + with gzip.open(gz, 'wb') as zipfile: while chunk := infile.read(64 * 1024): zipfile.write(chunk) - print(file + " compressed") + print(file + " compressed") + encrypt_file(key, gz, file + ".gz.enc") + print(file + " encrypted") pass diff --git a/crypt.py b/crypt.py index f4d2184..2a9c608 100644 --- a/crypt.py +++ b/crypt.py @@ -1,6 +1,11 @@ -import os, random, struct +import io +import os +import struct +import tempfile + from Crypto.Cipher import AES + def encrypt_file(key, in_file, out_file=None, chunksize=64 * 1024): """ Encrypts a file using AES (CBC mode) with the given key. @@ -22,22 +27,35 @@ def encrypt_file(key, in_file, out_file=None, chunksize=64 * 1024): sizes can be faster for some files and machines. chunksize must be divisible by 16. """ + if not out_file: out_file = in_file + '.enc' cipher = AES.new(key, AES.MODE_CBC) - filesize = os.path.getsize(in_file) - with open(in_file, 'rb') as infile: - with open(out_file, 'wb') as outfile: - outfile.write(struct.pack('