From 056a457c784f21c5765b61d6b8b2f1b7fbcdf884 Mon Sep 17 00:00:00 2001 From: Philipp Gesang Date: Tue, 30 May 2017 17:29:26 +0200 Subject: [PATCH] adapt file_crypt.py for revised crypto --- file_crypt.py | 30 +++++++++++++++++++++++++----- 1 files changed, 25 insertions(+), 5 deletions(-) diff --git a/file_crypt.py b/file_crypt.py index bd1f7dd..a5276b6 100755 --- a/file_crypt.py +++ b/file_crypt.py @@ -8,10 +8,28 @@ low-cost quick-devel badly-documented """ import sys +from deltatar import crypto from deltatar import tarfile from traceback import print_exc +CRYPTO_MODE_ENCRYPT = 0 +CRYPTO_MODE_DECRYPT = 1 + + +def initialize_encryption (mode, password=None, key=None, nacl=None): + if key is None and password is None: + raise Exception ("encryption requires either a key or a password") + if mode == CRYPTO_MODE_ENCRYPT: + return crypto.Encrypt (password=password, + key=key, + nacl=nacl, + version=crypto.PDTCRYPT_DEFAULT_VER, + paramversion=crypto.PDTCRYPT_DEFAULT_PVER) + if mode == CRYPTO_MODE_DECRYPT: + return crypto.Decrypt (password=password, key=key) + + def main(do_encrypt, in_file, out_file, password, comptype='gz', enctype='aes', bufsize=tarfile.RECORDSIZE, encoding='UTF-8'): """ Main function, called when running file as script @@ -27,18 +45,20 @@ def main(do_encrypt, in_file, out_file, password, comptype='gz', enctype='aes', if do_encrypt: read_handle = open(in_file, 'rt') else: - read_handle = tarfile._Stream(name=in_file, mode='r', + decryptor = initialize_encryption (CRYPTO_MODE_DECRYPT, + password=password) + read_handle = tarfile._Stream(name=in_file, mode="r", comptype=comptype, bufsize=bufsize, - fileobj=None, enctype=enctype, - password=password) + fileobj=None, encryption=decryptor) return_code = 3 # open file to write if do_encrypt: + encryptor = initialize_encryption (CRYPTO_MODE_ENCRYPT, + password=password) write_handle = tarfile._Stream(name=out_file, mode='w', comptype=comptype, bufsize=bufsize, - fileobj=None, enctype=enctype, - password=password) + fileobj=None, encryption=encryptor) else: write_handle = open(out_file, 'wt') return_code = 1 -- 1.7.1