From 012787a5b1368095b4f431ad2997d8212a8ff3eb Mon Sep 17 00:00:00 2001 From: Matt Nadareski Date: Mon, 8 Feb 2021 22:13:30 -0800 Subject: [PATCH] Hook up hashing, fix string output --- NDecrypt/Helper.cs | 8 ++++---- NDecrypt/Program.cs | 33 ++++++++++++++++++++++++++++++++- 2 files changed, 36 insertions(+), 5 deletions(-) diff --git a/NDecrypt/Helper.cs b/NDecrypt/Helper.cs index f06286e..4246e02 100644 --- a/NDecrypt/Helper.cs +++ b/NDecrypt/Helper.cs @@ -118,10 +118,10 @@ namespace NDecrypt // Get the results string result = $"Size: {size}\n" - + $"CRC32: {ByteArrayToString(hashers.First(h => h.HashType == Hash.CRC).GetHash()) ?? ""}" - + $"MD5: {ByteArrayToString(hashers.First(h => h.HashType == Hash.MD5).GetHash()) ?? ""}" - + $"SHA1: {ByteArrayToString(hashers.First(h => h.HashType == Hash.SHA1).GetHash()) ?? ""}" - + $"SHA256: {ByteArrayToString(hashers.First(h => h.HashType == Hash.SHA256).GetHash()) ?? ""}"; + + $"CRC32: {ByteArrayToString(hashers.First(h => h.HashType == Hash.CRC).GetHash()) ?? ""}\n" + + $"MD5: {ByteArrayToString(hashers.First(h => h.HashType == Hash.MD5).GetHash()) ?? ""}\n" + + $"SHA1: {ByteArrayToString(hashers.First(h => h.HashType == Hash.SHA1).GetHash()) ?? ""}\n" + + $"SHA256: {ByteArrayToString(hashers.First(h => h.HashType == Hash.SHA256).GetHash()) ?? ""}\n"; // Dispose of the hashers loadBuffer.Dispose(); diff --git a/NDecrypt/Program.cs b/NDecrypt/Program.cs index 7fc9108..eabb6b2 100644 --- a/NDecrypt/Program.cs +++ b/NDecrypt/Program.cs @@ -43,7 +43,7 @@ namespace NDecrypt return; } - bool development = false, force = false; + bool development = false, force = false, outputHashes = false; int start = 1; for ( ; start < args.Length; start++) { @@ -53,6 +53,9 @@ namespace NDecrypt else if (args[start] == "-f" || args[start] == "--force") force = true; + else if (args[start] == "-h" || args[start] == "--hash") + outputHashes = true; + else break; } @@ -65,6 +68,8 @@ namespace NDecrypt ITool tool = DeriveTool(args[i], encrypt.Value, development, force); if (tool?.ProcessFile() != true) Console.WriteLine("Processing failed!"); + else if (outputHashes) + WriteHashes(args[i]); } else if (Directory.Exists(args[i])) { @@ -74,6 +79,8 @@ namespace NDecrypt ITool tool = DeriveTool(file, encrypt.Value, development, force); if (tool?.ProcessFile() != true) Console.WriteLine("Processing failed!"); + else if (outputHashes) + WriteHashes(file); } } else @@ -101,6 +108,7 @@ d, decrypt - Decrypt the input files Possible values for [flags] (one or more can be used): -dev, --development - Enable using development keys, if available -f, --force - Force operation by avoiding sanity checks +-h, --hash - Output size and hashes to a companion file can be any file or folder that contains uncompressed items. More than one path can be specified at a time."); @@ -160,5 +168,28 @@ More than one path can be specified at a time."); return FileType.NULL; } + + /// + /// Write out the hashes of a file to a named file + /// + /// Filename to get hashes for/param> + private static void WriteHashes(string filename) + { + // If the file doesn't exist, don't try anything + if (!File.Exists(filename)) + return; + + // Get the hash string from the file + string hashString = Helper.GetInfo(filename); + if (hashString == null) + return; + + // Open the output file and write the hashes + using (var fs = File.Create(Path.GetFullPath(filename) + ".hash")) + using (var sw = new StreamWriter(fs)) + { + sw.WriteLine(hashString); + } + } } }