track data handled in crypto
authorPhilipp Gesang <philipp.gesang@intra2net.com>
Tue, 21 Mar 2017 14:11:32 +0000 (15:11 +0100)
committerPhilipp Gesang <philipp.gesang@intra2net.com>
Tue, 21 Mar 2017 14:11:32 +0000 (15:11 +0100)
deltatar/crypto.py
deltatar/tarfile.py

index 0428555..119bfbc 100755 (executable)
@@ -311,6 +311,9 @@ class Crypto (object):
     cnt  = None
     iv   = None
     password = None
+    stats = { "in"  : 0
+            , "out" : 0
+            , "obj" : 0 }
 
     def __init__ (self, *al, **akv):
         self.cnt  = 1
@@ -337,11 +340,17 @@ class Crypto (object):
 
     def process (self, buf):
         if self.aes is not None:
-            return self.aes.update (buf)
-        # this branch is taken when .close() is called on the fileobj
+            self.stats ["in"] += len (buf)
+            out = self.aes.update (buf)
+            self.stats ["out"] += len (out)
+            return out
         return b""
 
 
+    def counters (self):
+        return self.stats ["obj"], self.stats ["in"], self.stats ["out"]
+
+
 class Encrypt (Crypto):
 
     curobj = None
@@ -372,6 +381,7 @@ class Encrypt (Crypto):
         #self.aes.authenticate_additional_data (str.encode (aad))
 
         self.hdrdum = hdr_make_dummy (filename)
+        self.stats ["obj"] += 1
         return self.hdrdum
 
 
@@ -379,6 +389,7 @@ class Encrypt (Crypto):
         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 ()
+        self.stats ["out"] += len (data)
         self.ctsize += len (data)
         (filename, version, paramversion, nacl) = self.curobj
         ok, hdr = hdr_from_params (version, paramversion, nacl, self.iv,
@@ -421,6 +432,7 @@ class Decrypt (Crypto):
         #self.aes.authenticate_additional_data (str.encode (aad))
         ctsize = 0
         ptsize = 0
+        self.stats ["obj"] += 1
 
 
     def next_in_source (self, tarinfo, source):
@@ -432,13 +444,16 @@ class Decrypt (Crypto):
 
 
     def done (self, tag=None):
+        data = b""
         try:
             if tag is None:
-                return True, self.aes.finalize ()
+                ret, data = True, self.aes.finalize ()
             else:
-                return self.aes.finalize_with_tag (self.tag)
+                ret, data = self.aes.finalize_with_tag (self.tag)
         except crypto.cryptography.exceptions.InvalidTag as exn:
             return False, repr (exn)
+        self.stats ["out"] += len (data)
+        return ret, data
 
 
     def process (self, buf):
index 14b6b63..74f15e6 100644 (file)
@@ -531,6 +531,9 @@ class _Stream:
     def __del__(self):
         if hasattr(self, "closed") and not self.closed:
             self.close()
+        #if self.encryption is not None:
+        #    print("crypto: %d objects handled, %d B in, %d B out"
+        #          % self.encryption.counters ())
 
 
     def _init_write_encrypt (self, entry=None):