move ct length bookkeeping into encryptor
authorPhilipp Gesang <philipp.gesang@intra2net.com>
Fri, 17 Mar 2017 16:19:10 +0000 (17:19 +0100)
committerPhilipp Gesang <philipp.gesang@intra2net.com>
Fri, 17 Mar 2017 16:19:15 +0000 (17:19 +0100)
This saves us unreliable calculations over the _Stream progress
in tarfile.py.

deltatar/crypto.py

index a8807b6..32ee3dc 100755 (executable)
@@ -334,6 +334,7 @@ class Encrypt (Crypto):
 
     curobj = None
     hdrdum = None
+    ctsize = -1     # per object from .next() → .done()
 
     def __init__ (self, password, paramversion, nacl=None):
         if nacl is None:
@@ -350,6 +351,7 @@ class Encrypt (Crypto):
         self.iv = self.iv_make()
         self.curobj = (filename, version, paramversion, nacl)
         self.cnt += 1
+        self.ctsize = 0
         aad = "%s" % filename
         self.aes = Cipher \
                         ( algorithms.AES (self.key)
@@ -361,19 +363,25 @@ class Encrypt (Crypto):
         return self.hdrdum
 
 
-    def done (self, cmpdata, ctsize):
+    def done (self, cmpdata):
         if cmpdata != self.hdrdum:
             raise Exception ("XXX bad sync for writing header") ## we need to converge on a sensible error handling strategy
         data = self.aes.finalize ()
-        ctsize += len (data)
+        self.ctsize += len (data)
         (filename, version, paramversion, nacl) = self.curobj
         ok, hdr = hdr_from_params (version, paramversion, nacl, self.iv,
-                                   ctsize, self.aes.tag)
+                                   self.ctsize, self.aes.tag)
         if ok is False:
             raise Exception ("XXX error constructing header: %r" % hdr) ## we need to converge on a sensible error handling strategy
         return data, hdr
 
 
+    def process (self, buf):
+        data = super().process (buf)
+        self.ctsize += len (data)
+        return data
+
+
 class Decrypt (Crypto):
 
     pfx = None