util.py 777 B

123456789101112131415161718192021222324252627282930313233
  1. # Copyright (c) 2020, 2021 The Linux Foundation
  2. #
  3. # SPDX-License-Identifier: Apache-2.0
  4. import hashlib
  5. from west import log
  6. def getHashes(filePath):
  7. """
  8. Scan for and return hashes.
  9. Arguments:
  10. - filePath: path to file to scan.
  11. Returns: tuple of (SHA1, SHA256, MD5) hashes for filePath, or
  12. None if file is not found.
  13. """
  14. hSHA1 = hashlib.sha1()
  15. hSHA256 = hashlib.sha256()
  16. hMD5 = hashlib.md5()
  17. log.dbg(f" - getting hashes for {filePath}")
  18. try:
  19. with open(filePath, 'rb') as f:
  20. buf = f.read()
  21. hSHA1.update(buf)
  22. hSHA256.update(buf)
  23. hMD5.update(buf)
  24. except OSError:
  25. return None
  26. return (hSHA1.hexdigest(), hSHA256.hexdigest(), hMD5.hexdigest())