Skip to content

Commit

Permalink
add package hasher - skeleton
Browse files Browse the repository at this point in the history
  • Loading branch information
hlotyaks committed Jan 2, 2020
1 parent 8b02994 commit f7ae1f6
Show file tree
Hide file tree
Showing 6 changed files with 118 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"dotnet-test-explorer.testProjectPath": "GraphBuilder/tests"
"dotnet-test-explorer.testProjectPath": "*/tests"
}
32 changes: 32 additions & 0 deletions PackageHasher/src/HashAlgorithmExtensions.cs
Original file line number Diff line number Diff line change
@@ -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<byte[]> ComputeHashAsync(this HashAlgorithm alg, IEnumerable<FileInfo> 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;
}
}
}
47 changes: 47 additions & 0 deletions PackageHasher/src/PackageHasher.cs
Original file line number Diff line number Diff line change
@@ -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<string, string> HashFolders(List<string> folders)
{
Dictionary<string, string> hasedFolders = new Dictionary<string, string>();


return hasedFolders;
}
async Task<string> 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();

}
}
}
}
7 changes: 7 additions & 0 deletions PackageHasher/src/PackageHasher.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
</PropertyGroup>

</Project>
15 changes: 15 additions & 0 deletions PackageHasher/tests/PackageHaser.Tests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace PackageAnalyzer.PackageHasher.Tests
{

[TestClass]
public class PackageHasherTests
{
[TestMethod]
public void TestMethod1()
{
}
}

}
16 changes: 16 additions & 0 deletions PackageHasher/tests/tests.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp3.0</TargetFramework>

<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.2.0" />
<PackageReference Include="MSTest.TestAdapter" Version="1.4.0" />
<PackageReference Include="MSTest.TestFramework" Version="1.4.0" />
<PackageReference Include="coverlet.collector" Version="1.0.1" />
</ItemGroup>

</Project>

0 comments on commit f7ae1f6

Please sign in to comment.