From 2545fea2688788bf9b780c7878e28cc1d37fdbf8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marek=20Fi=C5=A1era?= Date: Mon, 17 Apr 2023 14:25:40 +0200 Subject: [PATCH] Fix after merge with main --- src/StaticWebAssetsSdk/Tasks/FileHasher.cs | 35 +++++++++++++++++++ ...osoft.NET.Sdk.StaticWebAssets.Tasks.csproj | 3 -- .../Tasks/ResolveCompressedAssets.cs | 1 - 3 files changed, 35 insertions(+), 4 deletions(-) create mode 100644 src/StaticWebAssetsSdk/Tasks/FileHasher.cs diff --git a/src/StaticWebAssetsSdk/Tasks/FileHasher.cs b/src/StaticWebAssetsSdk/Tasks/FileHasher.cs new file mode 100644 index 000000000000..7288e33ad28f --- /dev/null +++ b/src/StaticWebAssetsSdk/Tasks/FileHasher.cs @@ -0,0 +1,35 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +using System; +using System.Numerics; +using System.Security.Cryptography; +using System.Text; + +namespace Microsoft.AspNetCore.StaticWebAssets.Tasks; + +public static class FileHasher +{ + public static string GetFileHash(string filePath) + { + using var hash = SHA256.Create(); + var bytes = Encoding.UTF8.GetBytes(filePath); + var hashBytes = hash.ComputeHash(bytes); + return ToBase36(hashBytes); + } + + private static string ToBase36(byte[] hash) + { + const string chars = "0123456789abcdefghijklmnopqrstuvwxyz"; + + var result = new char[10]; + var dividend = BigInteger.Abs(new BigInteger(hash.AsSpan().Slice(0, 9).ToArray())); + for (var i = 0; i < 10; i++) + { + dividend = BigInteger.DivRem(dividend, 36, out var remainder); + result[i] = chars[(int)remainder]; + } + + return new string(result); + } +} diff --git a/src/StaticWebAssetsSdk/Tasks/Microsoft.NET.Sdk.StaticWebAssets.Tasks.csproj b/src/StaticWebAssetsSdk/Tasks/Microsoft.NET.Sdk.StaticWebAssets.Tasks.csproj index 8fe086a4e209..e8541a00012b 100644 --- a/src/StaticWebAssetsSdk/Tasks/Microsoft.NET.Sdk.StaticWebAssets.Tasks.csproj +++ b/src/StaticWebAssetsSdk/Tasks/Microsoft.NET.Sdk.StaticWebAssets.Tasks.csproj @@ -63,9 +63,6 @@ Shared\DotnetToolTask.cs - - Shared\FileHasher.cs - diff --git a/src/StaticWebAssetsSdk/Tasks/ResolveCompressedAssets.cs b/src/StaticWebAssetsSdk/Tasks/ResolveCompressedAssets.cs index e8c3e695c519..08f3cfeaf687 100644 --- a/src/StaticWebAssetsSdk/Tasks/ResolveCompressedAssets.cs +++ b/src/StaticWebAssetsSdk/Tasks/ResolveCompressedAssets.cs @@ -8,7 +8,6 @@ using Microsoft.Build.Framework; using Microsoft.Build.Utilities; using Microsoft.Extensions.FileSystemGlobbing; -using Microsoft.NET.Sdk.WebAssembly; namespace Microsoft.AspNetCore.StaticWebAssets.Tasks;