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