From 3cf214501067a2344dfec34783e07b34f1425db0 Mon Sep 17 00:00:00 2001 From: Martin Date: Tue, 3 Sep 2024 16:00:43 +0200 Subject: [PATCH] Restore MD5 hashes This commit restores the ComputeMD5Hash() array extensions to use MD5 internally again. Previously this has been changed to use SHA1 internally since the MD5 implementation is not supported on all platforms. See also: https://github.com/aardvark-platform/aardvark.base/commit/eb882338987827202e5d7f51e24e735f7daee145 https://github.com/aardvark-platform/aardvark.base/commit/64dd5663d477ca4c01d0fc4fb40ee91baab45066 https://github.com/aardvark-platform/aardvark.base/commit/b750926c2843ece70e6594eef4f59e9ca41a0220 --- .../Extensions/ArrayExtensions.cs | 62 +++++++++---------- .../Extensions/StringExtensions.cs | 16 +++-- .../Introspection/Introspection.cs | 17 +++-- src/Demo/Scratch/Store.fs | 6 -- 4 files changed, 53 insertions(+), 48 deletions(-) diff --git a/src/Aardvark.Base/Extensions/ArrayExtensions.cs b/src/Aardvark.Base/Extensions/ArrayExtensions.cs index a27064b5..81f75a71 100644 --- a/src/Aardvark.Base/Extensions/ArrayExtensions.cs +++ b/src/Aardvark.Base/Extensions/ArrayExtensions.cs @@ -1,7 +1,6 @@ using System; using System.Collections.Generic; using System.IO; -using System.Linq; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; using System.Security.Cryptography; @@ -2336,7 +2335,7 @@ public static Span AsByteSpan(this T[] data) where T : struct { return MemoryMarshal.AsBytes(data.AsSpan()); } - + [MethodImpl(MethodImplOptions.AggressiveInlining)] public static ReadOnlySpan AsByteSpan(this string data) { @@ -2392,6 +2391,8 @@ internal static unsafe void UseAsStream(this Array data, Action /// Computes the MD5 hash of the data array. /// @@ -2399,19 +2400,13 @@ internal static unsafe void UseAsStream(this Array data, Action /// Computes the MD5 hash of the data array. /// @@ -2419,17 +2414,10 @@ public static byte[] ComputeMD5Hash(this byte[] data) public static byte[] ComputeMD5Hash(this Array data) { #if NET6_0_OR_GREATER - var hash = SHA1.HashData(data.AsByteSpan()); - Array.Resize(ref hash, 16); - return hash; + return MD5.HashData(data.AsByteSpan()); #else - - using(var sha = SHA1.Create()) - { - var hash = data.UseAsStream((stream) => sha.ComputeHash(stream)); - Array.Resize(ref hash, 16); - return hash; - } + using var md5 = MD5.Create(); + return data.UseAsStream((stream) => md5.ComputeHash(stream)); #endif } @@ -2439,11 +2427,7 @@ public static byte[] ComputeMD5Hash(this Array data) /// /// 128bit/16byte data hash public static byte[] ComputeMD5Hash(this T[] data) where T : struct - { - var hash = SHA1.HashData(data.AsByteSpan()); - Array.Resize(ref hash, 16); - return hash; - } + => MD5.HashData(data.AsByteSpan()); #endif /// @@ -2453,14 +2437,16 @@ public static byte[] ComputeMD5Hash(this T[] data) where T : struct public static byte[] ComputeMD5Hash(this string s) { #if NET6_0_OR_GREATER - var hash = SHA1.HashData(s.AsByteSpan()); - Array.Resize(ref hash, 16); - return hash; + return MD5.HashData(s.AsByteSpan()); #else return Encoding.Unicode.GetBytes(s).ComputeMD5Hash(); #endif } + #endregion + + #region SHA1 + /// /// Computes the SHA1 hash of the data array. /// @@ -2518,6 +2504,10 @@ public static byte[] ComputeSHA1Hash(this string s) #endif } + #endregion + + #region SHA256 + /// /// Computes the SHA256 hash of the data array. /// @@ -2575,6 +2565,10 @@ public static byte[] ComputeSHA256Hash(this string s) #endif } + #endregion + + #region SHA512 + /// /// Computes the SHA512 hash of the data array. /// @@ -2632,6 +2626,10 @@ public static byte[] ComputeSHA512Hash(this string s) #endif } + #endregion + + #region Adler32 + /// /// Computes a checksum of the data array using the Adler-32 algorithm (). /// @@ -2687,6 +2685,8 @@ public static uint ComputeAdler32Checksum(this string s) return a.Checksum; } -#endregion + #endregion + + #endregion } } \ No newline at end of file diff --git a/src/Aardvark.Base/Extensions/StringExtensions.cs b/src/Aardvark.Base/Extensions/StringExtensions.cs index 97c39328..a3683f24 100644 --- a/src/Aardvark.Base/Extensions/StringExtensions.cs +++ b/src/Aardvark.Base/Extensions/StringExtensions.cs @@ -363,16 +363,22 @@ public static IEnumerable NestedBracketSplit(this string text, int split #region Guid /// - /// Computes the MD5 hash of the given string and returns it as Guid. + /// Computes the SHA1 hash of the given string and returns it as Guid. /// public static Guid ToGuid(this string self) { if (string.IsNullOrEmpty(self)) return Guid.Empty; - return new Guid(self.ComputeMD5Hash()); + var hash = self.ComputeSHA1Hash(); +#if NET8_0_OR_GREATER + return new Guid(hash.AsSpan(0, 16)); +#else + Array.Resize(ref hash, 16); + return new Guid(hash); +#endif } /// - /// Combines the given strings and returns a Guid based on the MD5 hash. + /// Combines the given strings and returns a Guid based on the SHA1 hash. /// public static Guid ToGuid(this IEnumerable self) { @@ -382,7 +388,7 @@ public static Guid ToGuid(this IEnumerable self) } /// - /// Combines the given Guids to a string and returns a Guid based on the MD5 hash. + /// Combines the given Guids to a string and returns a Guid based on the SHA1 hash. /// public static Guid ToGuid(this IEnumerable self) { @@ -391,7 +397,7 @@ public static Guid ToGuid(this IEnumerable self) return sb.ToString().ToGuid(); } - #endregion +#endregion #region Properties diff --git a/src/Aardvark.Base/Introspection/Introspection.cs b/src/Aardvark.Base/Introspection/Introspection.cs index 30f81c92..25e64d30 100644 --- a/src/Aardvark.Base/Introspection/Introspection.cs +++ b/src/Aardvark.Base/Introspection/Introspection.cs @@ -10,6 +10,7 @@ using System.Reflection.PortableExecutable; using System.Runtime.InteropServices; using System.Runtime.Serialization; +using System.Security.Cryptography; using System.Text; using System.Text.RegularExpressions; using System.Xml; @@ -1717,14 +1718,18 @@ public static bool TryGetNativeLibraryPath(Assembly assembly, out string path) if (SeparateLibraryDirectories) { - var md5 = System.Security.Cryptography.SHA1.Create(); - var bytes = md5.ComputeHash(s); - Array.Resize(ref bytes, 16); - var hash = new Guid(bytes); - md5.Dispose(); +#if NET8_0_OR_GREATER + var hash = SHA1.HashData(s); + var guid = new Guid(hash.AsSpan(0, 16)); +#else + using var sha1 = SHA1.Create(); + var hash = sha1.ComputeHash(s); + Array.Resize(ref hash, 16); + var guid = new Guid(hash); +#endif GetPlatformAndArch(out var platform, out var arch); - dstFolder = Path.Combine(dstFolder, assembly.GetName().Name, hash.ToString(), platform, arch); + dstFolder = Path.Combine(dstFolder, assembly.GetName().Name, guid.ToString(), platform, arch); } s_nativePaths[assembly] = dstFolder; diff --git a/src/Demo/Scratch/Store.fs b/src/Demo/Scratch/Store.fs index 392b2378..ba22a10b 100644 --- a/src/Demo/Scratch/Store.fs +++ b/src/Demo/Scratch/Store.fs @@ -15,12 +15,6 @@ open System.Threading #nowarn "9" -let hash = SHA1.Create() -let md5 (str : string) = - str |> Encoding.Unicode.GetBytes |> hash.ComputeHash |> Array.truncate 16 |> Guid - - - module FileManagerTypes = let inline (++) (ptr : nativeptr<'a>) (v : 'a) = NativePtr.add ptr (int v) let inline (!!) (ptr : nativeptr<'a>) = NativePtr.read ptr