########################################################################### # # Copyright (c) 2021 Diality Inc. - All Rights Reserved. # # THIS CODE MAY NOT BE COPIED OR REPRODUCED IN ANY FORM, IN PART OR IN # WHOLE, WITHOUT THE EXPLICIT PERMISSION OF THE COPYRIGHT OWNER. # # @file files_check.py # # @author (last) Peter Lucia # @date (last) 05-May-2021 # @author (original) Peter Lucia # @date (original) 05-May-2021 # ############################################################################ import os import json import socket import glob from hashlib import sha256 from typing import List from collections import OrderedDict def generate_sha256(path: str) -> str: """ Generates a sha256 checksum for the provided file :param path: the file to generate :return: the checksum """ if not os.path.exists(path): print("Invalid path.") return "" return sha256(open(path,'rb').read()).hexdigest() def check_application(path: str, checksum: str) -> bool: """ Checks that the application is not corrupted and has the correct signature :param path: the path to the application binary :param checksum: the expected sha256 hash for the application :return: True if pass, False otherwise """ return generate_sha256(path) == checksum def generate_checksums(files: List[str], outfile: str) -> None: """ Generates checksums for the list of files :param files: the list of files to generate checksums for :param outfile: where to write the resultant json file :return: None """ lookup = OrderedDict() for file in files: lookup[file] = generate_sha256(file) print(lookup) with open(outfile, 'w', encoding='utf-8') as f: json.dump(lookup, f, ensure_ascii=False, indent=4) def get_files_in_dir(base_dir: str, file_types: str = "*.sh", recursive: bool = False) -> List[str]: """ Gets a list of full paths for each script placed on the device :param base_dir: the parent directory where the scripts reside :param file_types: the types of files we want to look at :param recursive: if True, recurse, otherwise only look in base_dir :return: a list of all the scripts """ return glob.glob(os.path.join(base_dir, file_types), recursive=recursive) def get_files_recursive(base_dir: str) -> List[str]: """ Recursively gets a list of full paths for each file in the provided base directory :return: the list of all the files """ return [os.path.join(dp, f) for dp, dn, fn in os.walk(os.path.expanduser(base_dir)) for f in fn] if __name__ == '__main__': hostname = socket.gethostname() if hostname == "DEN-UI": files = [ "/home/denali/Projects/tmp/build/denali-Qt_5_12_5_iMX8-Release/denali", ] files += get_files_in_dir("/home/denali/Projects/application/scripts") generate_checksums(files, "checksums_desktop.conf") elif hostname == "b2qt-nitrogen8mm": files = [] files += get_files_in_dir("/home/root/") files += get_files_in_dir("/home/root/scripts/") files += get_files_recursive("/bin/") files += get_files_recursive("/sbin/") files += get_files_recursive("/lib/") generate_checksums(files, "checksums_target.conf")