/* * Copyright (c) 2022 Winsider Seminars & Solutions, Inc. All rights reserved. * * This file is part of System Informer. * * Authors: * * jxy-s * */ namespace CustomBuildTool { /// /// Provides methods for managing and verifying the build tools identity hash. /// public static class BuildToolsId { /// /// Checks if the build tools are out of date by comparing the current tools hash /// with the previously stored hash in the ToolsId.txt file. /// Prints a warning message if the hashes do not match. /// public static void CheckForOutOfDateTools() { #if RELEASE string directory = Path.GetDirectoryName(Environment.ProcessPath); string fileameId = Path.Join([directory, "\\ToolsId.txt"]); string currentId = GetToolsId(); string previousId = string.Empty; if (File.Exists(fileameId)) { previousId = Utils.ReadAllText(fileameId); } if (string.IsNullOrWhiteSpace(previousId) || !previousId.Equals(currentId, StringComparison.OrdinalIgnoreCase)) { Program.PrintColorMessage($"[WARNING] Build tools are out of date!", ConsoleColor.Yellow); } #endif } /// /// Writes the current build tools hash to the ToolsId.txt file /// and prints the hash to the console. /// public static void WriteToolsId() { string directory = Path.GetDirectoryName(Environment.ProcessPath); string parentDirectory = Path.GetDirectoryName(directory); string processName = Path.GetFileName(Environment.ProcessPath); string currentHash = GetToolsId(); Utils.WriteAllText(Path.Join([directory, "\\ToolsId.txt"]), currentHash); // // Write the tools id for the other published architectures (x86, arm64) // since the host can only execute the binary for its own architecture. // if (!string.IsNullOrWhiteSpace(parentDirectory) && Directory.Exists(parentDirectory)) { foreach (string archDirectory in Directory.EnumerateDirectories(parentDirectory)) { if (string.Equals(archDirectory, directory, StringComparison.OrdinalIgnoreCase)) continue; if (!File.Exists(Path.Join([archDirectory, processName]))) continue; Utils.WriteAllText(Path.Join([archDirectory, "\\ToolsId.txt"]), currentHash); } } Program.PrintColorMessage("Tools Hash: ", ConsoleColor.Gray, false); Program.PrintColorMessage(currentHash, ConsoleColor.Green); } /// /// Computes an SHA-256 hash of all .cs files in the specified build tool directories. /// The hash represents the current state of the build tools source files. /// /// /// A hexadecimal string representing the SHA-256 hash of the build tools source files, /// or an empty string if the hash could not be computed. /// private static string GetToolsId() { string[] directories = [ "tools\\CustomBuildTool", "tools\\CustomBuildTool\\AzureSignTool", ]; return GetToolsId(directories); } private static IEnumerable GetAllCsFiles(string[] Directories) { foreach (string directory in Directories) { foreach (string source in Directory.EnumerateFiles(directory, "*.cs", SearchOption.TopDirectoryOnly)) { yield return source; } } } private static string GetToolsId(string[] Directories) { const int bufferSize = 0x4000; using var sha256 = SHA256.Create(); byte[] buffer = ArrayPool.Shared.Rent(bufferSize); try { foreach (string source in GetAllCsFiles(Directories)) { using (var filestream = new FileStream(source, FileMode.Open, FileAccess.Read, FileShare.Read, bufferSize, FileOptions.SequentialScan)) { int bytesRead; while ((bytesRead = filestream.Read(buffer, 0, buffer.Length)) > 0) { sha256.TransformBlock(buffer, 0, bytesRead, null, 0); } } } } finally { ArrayPool.Shared.Return(buffer, clearArray: true); } sha256.TransformFinalBlock(Array.Empty(), 0, 0); return sha256.Hash == null ? string.Empty : Convert.ToHexString(sha256.Hash); } } }