"""
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
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