adapt file_crypt.py for revised crypto
authorPhilipp Gesang <philipp.gesang@intra2net.com>
Tue, 30 May 2017 15:29:26 +0000 (17:29 +0200)
committerThomas Jarosch <thomas.jarosch@intra2net.com>
Mon, 2 Apr 2018 11:34:09 +0000 (13:34 +0200)
file_crypt.py

index bd1f7dd..a5276b6 100755 (executable)
@@ -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