timecapsule

share keys / encrypt / decrypt files for pseudo time-capsule crypto
git clone https://0x1A4.1337.cx/code2/timecapsule.git
Log | Files | Refs | LICENSE

commit 07374498df811fcc4dac7998e2d21c436d42dfd7
parent 316fe61fc4ed50e44547c3439abbd23a837a7da5
Author: tx <traxteur@gmail.com>
Date:   Thu Mar 30 20:19:14 +0200

use temporary file for extraction

Diffstat:
time-capsule.py | 36+++++++++++++++++++++---------------
1 file changed, 21 insertions(+), 15 deletions(-)
diff --git a/time-capsule.py b/time-capsule.py @@ -97,6 +97,7 @@ class TimeCapsule(object): return 'public'.join(arr[:-1]) + 'private' + arr[-1] def encrypt(self, file, pub_url, priv_url = None): + name = os.path.basename(file) # gen priv url if not given if priv_url == None: priv_url = self.gen_priv_url(pub_url) @@ -115,16 +116,15 @@ class TimeCapsule(object): exit(1) # crypt file - cfile = f'{file}.rsa' try: - with open(file, 'rb') as infile, open(cfile, 'wb') as outfile: + with open(file, 'rb') as infile, tempfile.NamedTemporaryFile(delete=False) as outfile: encrypt_bigfile(infile, outfile, pubkey) except: sys.stderr.write(f'Unable to encrypt {file}\n') sys.exit(1) # create tcrypt archive - self.tar_cypherfile(cfile, priv_url) + self.tar_cypherfile(outfile.name, name, priv_url) # return date from key url def get_date_from_key_url(self, key_url): @@ -153,20 +153,20 @@ key = {url} return readme.name # tar a cypherfile with metadatas - def tar_cypherfile(self, file, priv_url): + def tar_cypherfile(self, file, name, priv_url): readme = self.create_readme(priv_url) - parc = os.path.splitext(file)[0] + '.tcrypt' + parc = f'{name}.tcrypt' with tarfile.open(parc, 'w') as tar: - tar.add(file) + tar.add(file, arcname=name) tar.add(readme, arcname='README') os.remove(file) os.remove(readme) # returns a meta dictionary from a json meta file - def load_readme(self, file): + def load_readme(self, path): config = configparser.ConfigParser() try: - config.read(file) + config.read(path) meta = {'date': config['ARCHIVE']['date'], 'key': config['ARCHIVE']['key']} except Exception as e: print(e) @@ -177,9 +177,15 @@ key = {url} # decrypt a .tcrypt archive def decrypt(self, file, priv_url = None): # untar + tempdir = tempfile.TemporaryDirectory().name + with tarfile.open(file) as tar: - tar.extractall() - meta = self.load_readme('README') + tar.extractall(path=tempdir) + for item in os.listdir(tempdir): + if item == 'README': + meta = self.load_readme(os.path.join(tempdir, item)) + else: + tpath = os.path.join(tempdir, item) # get private key url if not specified in command if priv_url == None: @@ -201,15 +207,15 @@ key = {url} # decrypt file fout = os.path.splitext(file)[0] - fin = fout + '.rsa' try: - with open(fin, 'rb') as infile, open(fout, 'wb') as outfile: + with open(tpath, 'rb') as infile, open(fout, 'wb') as outfile: decrypt_bigfile(infile, outfile, privkey) - except: + except Exception as e: + sys.stderr.write(e) sys.stderr.write(f'Unable to decrypt {file}\n') sys.exit(1) - os.remove('README') - os.remove(fin) + + shutil.rmtree(tempdir) if __name__ == '__main__': fire.Fire(TimeCapsule)