From: Michael Tremer Date: Fri, 15 Jul 2022 09:53:57 +0000 (+0000) Subject: uploads: Add simple function to compute digest X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=80d756c8685b24085bd17853c30f32bcdf3b092d;p=pbs.git uploads: Add simple function to compute digest Signed-off-by: Michael Tremer --- diff --git a/src/buildservice/uploads.py b/src/buildservice/uploads.py index d79c0f71..d8e6bf5a 100644 --- a/src/buildservice/uploads.py +++ b/src/buildservice/uploads.py @@ -1,8 +1,8 @@ #!/usr/bin/python3 +import hashlib import logging import os -import shutil import tempfile from . import base @@ -159,3 +159,25 @@ class Upload(base.DataObject): @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()