From f7ae1f62f9ec6d5ed95deea41e75d7058d2a5aca Mon Sep 17 00:00:00 2001 From: hlotyaks Date: Thu, 2 Jan 2020 13:02:56 -0500 Subject: [PATCH] add package hasher - skeleton --- .vscode/settings.json | 2 +- PackageHasher/src/HashAlgorithmExtensions.cs | 32 +++++++++++++ PackageHasher/src/PackageHasher.cs | 47 ++++++++++++++++++++ PackageHasher/src/PackageHasher.csproj | 7 +++ PackageHasher/tests/PackageHaser.Tests.cs | 15 +++++++ PackageHasher/tests/tests.csproj | 16 +++++++ 6 files changed, 118 insertions(+), 1 deletion(-) create mode 100644 PackageHasher/src/HashAlgorithmExtensions.cs create mode 100644 PackageHasher/src/PackageHasher.cs create mode 100644 PackageHasher/src/PackageHasher.csproj create mode 100644 PackageHasher/tests/PackageHaser.Tests.cs create mode 100644 PackageHasher/tests/tests.csproj diff --git a/.vscode/settings.json b/.vscode/settings.json index fb99111..7c7d608 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,3 +1,3 @@ { - "dotnet-test-explorer.testProjectPath": "GraphBuilder/tests" + "dotnet-test-explorer.testProjectPath": "*/tests" } \ No newline at end of file diff --git a/PackageHasher/src/HashAlgorithmExtensions.cs b/PackageHasher/src/HashAlgorithmExtensions.cs new file mode 100644 index 0000000..9e1ccb8 --- /dev/null +++ b/PackageHasher/src/HashAlgorithmExtensions.cs @@ -0,0 +1,32 @@ +using System.Text; +using System.Security.Cryptography; +using System.IO; +using System.Collections.Generic; +using System.Threading.Tasks; + +namespace PackageAnalyzer +{ + public static class HashAlgorithmExtensions + { + public static async Task ComputeHashAsync(this HashAlgorithm alg, IEnumerable files, string rootPath) + { + using (var cs = new CryptoStream(Stream.Null, alg, CryptoStreamMode.Write)) + { + foreach (var file in files) + { + // Will either include the rootPath or not. + string fileName = file.FullName.Substring(rootPath.Length, file.FullName.Length - rootPath.Length); + var pathBytes = Encoding.UTF8.GetBytes(fileName); + cs.Write(pathBytes, 0, pathBytes.Length); + + using (var fs = file.OpenRead()) + await fs.CopyToAsync(cs); + } + + cs.FlushFinalBlock(); + } + + return alg.Hash; + } + } +} \ No newline at end of file diff --git a/PackageHasher/src/PackageHasher.cs b/PackageHasher/src/PackageHasher.cs new file mode 100644 index 0000000..cf13fbb --- /dev/null +++ b/PackageHasher/src/PackageHasher.cs @@ -0,0 +1,47 @@ +using System; +using System.Text; +using System.IO; +using System.Threading.Tasks; +using System.Security.Cryptography; +using System.Linq; + +namespace PackageAnalyzer +{ + public class PackageHasher + { + public Dictionary HashFolders(List folders) + { + Dictionary hasedFolders = new Dictionary(); + + + return hasedFolders; + } + async Task HashFolder(DirectoryInfo folder, bool excludeRoot, string searchPattern = "*", SearchOption searchOption = SearchOption.AllDirectories) + { + string rootPath = String.Empty; + + if(excludeRoot) + { + rootPath = folder.FullName.Substring(0, folder.FullName.Length - folder.FullName.Split('\\').Last().Length ); + } + + using(var alg = MD5.Create()) + { + + var result = await alg.ComputeHashAsync(folder.EnumerateFiles(searchPattern, searchOption), rootPath); + + // Build the final string by converting each byte + // into hex and appending it to a StringBuilder + StringBuilder sb = new StringBuilder(); + for (int i = 0; i < result.Length; i++) + { + sb.Append(result[i].ToString("X2")); + } + + // And return it + return sb.ToString(); + + } + } + } +} diff --git a/PackageHasher/src/PackageHasher.csproj b/PackageHasher/src/PackageHasher.csproj new file mode 100644 index 0000000..9f5c4f4 --- /dev/null +++ b/PackageHasher/src/PackageHasher.csproj @@ -0,0 +1,7 @@ + + + + netstandard2.0 + + + diff --git a/PackageHasher/tests/PackageHaser.Tests.cs b/PackageHasher/tests/PackageHaser.Tests.cs new file mode 100644 index 0000000..60121c7 --- /dev/null +++ b/PackageHasher/tests/PackageHaser.Tests.cs @@ -0,0 +1,15 @@ +using Microsoft.VisualStudio.TestTools.UnitTesting; + +namespace PackageAnalyzer.PackageHasher.Tests +{ + + [TestClass] + public class PackageHasherTests + { + [TestMethod] + public void TestMethod1() + { + } + } + +} \ No newline at end of file diff --git a/PackageHasher/tests/tests.csproj b/PackageHasher/tests/tests.csproj new file mode 100644 index 0000000..8ae4c5b --- /dev/null +++ b/PackageHasher/tests/tests.csproj @@ -0,0 +1,16 @@ + + + + netcoreapp3.0 + + false + + + + + + + + + +