From 1438ec295871c97dedbed87b871f6963e5fb0ced Mon Sep 17 00:00:00 2001 From: vletroye Date: Sat, 16 Jan 2021 18:10:23 +0100 Subject: [PATCH] Isolate the Checksum computation --- Hash/App.config | 6 ++++ Hash/Hash.csproj | 62 +++++++++++++++++++++++++++++++++ Hash/Program.cs | 57 ++++++++++++++++++++++++++++++ Hash/Properties/AssemblyInfo.cs | 36 +++++++++++++++++++ 4 files changed, 161 insertions(+) create mode 100644 Hash/App.config create mode 100644 Hash/Hash.csproj create mode 100644 Hash/Program.cs create mode 100644 Hash/Properties/AssemblyInfo.cs diff --git a/Hash/App.config b/Hash/App.config new file mode 100644 index 0000000..bae5d6d --- /dev/null +++ b/Hash/App.config @@ -0,0 +1,6 @@ + + + + + + diff --git a/Hash/Hash.csproj b/Hash/Hash.csproj new file mode 100644 index 0000000..4d9166a --- /dev/null +++ b/Hash/Hash.csproj @@ -0,0 +1,62 @@ + + + + + Debug + AnyCPU + {A88C4996-406E-4F26-BEE2-1D7ACB2BAB21} + Exe + Properties + Hash + Hash + v4.6.1 + 512 + true + + + + AnyCPU + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + AnyCPU + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/Hash/Program.cs b/Hash/Program.cs new file mode 100644 index 0000000..c6b6429 --- /dev/null +++ b/Hash/Program.cs @@ -0,0 +1,57 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Security.Cryptography; +using System.Text; +using System.Threading.Tasks; +using System.Windows.Forms; + +namespace Hash +{ + class Program + { + static void Main(string[] args) + { + var path = "."; + if (args.Length > 0) + path = args[0]; + if (path == ".") path = AppDomain.CurrentDomain.BaseDirectory; + ComputeMD5Hash(path); + } + + static void ComputeMD5Hash(string path) + { + string hash = ""; + var package = Path.Combine(path, "package.tgz"); + var info = Path.Combine(path, "INFO"); + + using (var md5 = MD5.Create()) + { + using (var stream = File.OpenRead(package)) + { + var hashByte = md5.ComputeHash(stream); + hash = BitConverter.ToString(hashByte).Replace("-", "").ToLower(); + } + } + + if (File.Exists(info)) + { + var lines = File.ReadAllLines(info); + using (StreamWriter outputFile = new StreamWriter(info)) + { + foreach (var line in lines) + { + var key = line.Substring(0, line.IndexOf('=')); + var value = line.Substring(line.IndexOf('=') + 1); + value = value.Trim(new char[] { '"' }); + + if (key != "checksum") + outputFile.WriteLine("{0}=\"{1}\"", key, value); + } + outputFile.WriteLine("checksum=\"{0}\"", hash); + } + } + } + } +} diff --git a/Hash/Properties/AssemblyInfo.cs b/Hash/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..a354022 --- /dev/null +++ b/Hash/Properties/AssemblyInfo.cs @@ -0,0 +1,36 @@ +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +// General Information about an assembly is controlled through the following +// set of attributes. Change these attribute values to modify the information +// associated with an assembly. +[assembly: AssemblyTitle("Hash")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("Hash")] +[assembly: AssemblyCopyright("Copyright © 2021")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// Setting ComVisible to false makes the types in this assembly not visible +// to COM components. If you need to access a type in this assembly from +// COM, set the ComVisible attribute to true on that type. +[assembly: ComVisible(false)] + +// The following GUID is for the ID of the typelib if this project is exposed to COM +[assembly: Guid("a88c4996-406e-4f26-bee2-1d7acb2bab21")] + +// Version information for an assembly consists of the following four values: +// +// Major Version +// Minor Version +// Build Number +// Revision +// +// You can specify all the values or you can default the Build and Revision Numbers +// by using the '*' as shown below: +// [assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")]