using SharpExifTool; using System.Security.Cryptography; namespace Shadow.Tools; public static class MetadataExtractor { private readonly static ExifTool exifTool = new(); private readonly static GeneralUseHelpers guhf = new(); public async static Task> ExtractAsync(string fullPath) { // Get all relevant metadata Dictionary fileMetadata = new(await exifTool.ExtractAllMetadataAsync(fullPath)); // Add in a MD5 hint string md5sum = await GetFileMD5Async(fullPath); fileMetadata?.Add("_shadow:fileHash", md5sum); return fileMetadata ?? []; } /// /// Compute MD5 checksum of a file /// /// Input file absolute path /// MD5 hexstring public static async Task GetFileMD5Async(string fullPath) { string fallbackValue = String.Empty; try { if (File.Exists(fullPath)) using (MD5 md5 = MD5.Create()) { using (FileStream stream = File.OpenRead(fullPath)) { byte[] hashBytes = await md5.ComputeHashAsync(stream); string hashString = Convert.ToHexStringLower(hashBytes); return hashString; } } else return fallbackValue; } catch { return fallbackValue; } } /// /// Compute MD5 checksum of a string /// /// Input string for the MD5 hash /// MD5 hexstring public static string GetStringMD5(string input) { // https://stackoverflow.com/a/24031467 byte[] inputBytes = System.Text.Encoding.ASCII.GetBytes(input); byte[] hashBytes = MD5.HashData(inputBytes); string hashString = Convert.ToHexStringLower(hashBytes); return hashString; } }