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