add a validate method to HashedStream
authorHelmut Grohne <helmut@subdivi.de>
Sat, 16 Apr 2016 09:22:18 +0000 (11:22 +0200)
committerHelmut Grohne <helmut@subdivi.de>
Sat, 16 Apr 2016 09:22:18 +0000 (11:22 +0200)
dedup/hashing.py
importpkg.py

index 1419811..2a83929 100644 (file)
@@ -183,3 +183,13 @@ class HashedStream(object):
 
     def hexdigest(self):
         return self.hashobj.hexdigest()
+
+    def validate(self, hexdigest):
+        """Soak up any remaining input and validate the read data using the
+        given hexdigest.
+        @raises ValueError: when the hash does not match
+        """
+        while self.read(65536):
+            pass
+        if self.hexdigest() != hexdigest:
+            raise ValueError("hash sum mismatch")
index 2cdefc4..f72cf03 100755 (executable)
@@ -107,10 +107,7 @@ def main():
         stdin = HashedStream(stdin, hashlib.sha256())
     process_package(stdin, hash_functions, dumper.represent)
     if options.hash:
-        while stdin.read(4096):
-            pass
-        if stdin.hexdigest() != options.hash:
-            raise ValueError("hash sum mismatch")
+        stdin.validate(options.hash)
     dumper.represent("commit")
     dumper.close()