Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
#!/usr/bin/python3
+import hashlib
import logging
import os
-import shutil
import tempfile
from . import base
@property
def expires_at(self):
return self.data.expires_at
+
+ async def digest(self, algo):
+ """
+ Computes the digest of this download
+ """
+ log.debug("Computing '%s' digest for %s" % (algo, self))
+
+ return await asyncio.to_thread(self._digest, algo)
+
+ def _digest(self, algo):
+ h = hashlib.new(algo)
+
+ # Read the entire file and pass it to the hash function
+ with open(self.path, "rb") as f:
+ buf = f.read(4096)
+ if not buf:
+ break
+
+ h.update(buf)
+
+ # Return the digest
+ return h.digest()