digest.py 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. #!/usr/bin/env python3
  2. # Contest Management System - http://cms-dev.github.io/
  3. # Copyright © 2018 Stefano Maggiolo <s.maggiolo@gmail.com>
  4. #
  5. # This program is free software: you can redistribute it and/or modify
  6. # it under the terms of the GNU Affero General Public License as
  7. # published by the Free Software Foundation, either version 3 of the
  8. # License, or (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU Affero General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU Affero General Public License
  16. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. import hashlib
  18. import io
  19. from cmscommon.binary import bin_to_hex
  20. __all__ = [
  21. "Digester", "bytes_digest", "path_digest"
  22. ]
  23. class Digester:
  24. """Simple wrapper of hashlib using our preferred hasher."""
  25. def __init__(self):
  26. self._hasher = hashlib.sha1()
  27. def update(self, b):
  28. """Add the bytes b to the hasher."""
  29. self._hasher.update(b)
  30. def digest(self):
  31. """Return the digest as an hex string."""
  32. return bin_to_hex(self._hasher.digest())
  33. def bytes_digest(b):
  34. """Return the digest for the passed bytes.
  35. Currently CMS uses SHA1, but this should be treated as an implementation
  36. detail.
  37. b (bytes): some bytes.
  38. return (str): digest of the bytes.
  39. """
  40. d = Digester()
  41. d.update(b)
  42. return d.digest()
  43. def path_digest(path):
  44. """Return the digest of the content of a file, given by its path.
  45. path (str): path of the file we are interested in.
  46. return (str): digest of the content of the file in path.
  47. """
  48. with open(path, 'rb') as fin:
  49. d = Digester()
  50. buf = fin.read(io.DEFAULT_BUFFER_SIZE)
  51. while len(buf) > 0:
  52. d.update(buf)
  53. buf = fin.read(io.DEFAULT_BUFFER_SIZE)
  54. return d.digest()