Skip to content

Commit

Permalink
Restore MD5 hashes
Browse files Browse the repository at this point in the history
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:

eb88233
64dd566
b750926
  • Loading branch information
hyazinthh committed Sep 3, 2024
1 parent cb5e514 commit 3cf2145
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 48 deletions.
62 changes: 31 additions & 31 deletions src/Aardvark.Base/Extensions/ArrayExtensions.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -2336,7 +2335,7 @@ public static Span<byte> AsByteSpan<T>(this T[] data) where T : struct
{
return MemoryMarshal.AsBytes(data.AsSpan());
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static ReadOnlySpan<byte> AsByteSpan(this string data)
{
Expand Down Expand Up @@ -2392,44 +2391,33 @@ internal static unsafe void UseAsStream(this Array data, Action<UnmanagedMemoryS

#region Hashes

#region MD5

/// <summary>
/// Computes the MD5 hash of the data array.
/// </summary>
/// <returns>128bit/16byte data hash</returns>
public static byte[] ComputeMD5Hash(this byte[] data)
{
#if NET6_0_OR_GREATER
var hash = SHA1.HashData(data);
Array.Resize(ref hash, 16);
return hash;
return MD5.HashData(data);
#else
using (var sha = SHA1.Create())
{
var hash = sha.ComputeHash(data);
Array.Resize(ref hash, 16);
return hash;
}
using var md5 = MD5.Create();
return md5.ComputeHash(data);
#endif
}

/// <summary>
/// Computes the MD5 hash of the data array.
/// </summary>
/// <returns>128bit/16byte data hash</returns>
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
}

Expand All @@ -2439,11 +2427,7 @@ public static byte[] ComputeMD5Hash(this Array data)
/// </summary>
/// <returns>128bit/16byte data hash</returns>
public static byte[] ComputeMD5Hash<T>(this T[] data) where T : struct
{
var hash = SHA1.HashData(data.AsByteSpan());
Array.Resize(ref hash, 16);
return hash;
}
=> MD5.HashData(data.AsByteSpan());
#endif

/// <summary>
Expand All @@ -2453,14 +2437,16 @@ public static byte[] ComputeMD5Hash<T>(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

/// <summary>
/// Computes the SHA1 hash of the data array.
/// </summary>
Expand Down Expand Up @@ -2518,6 +2504,10 @@ public static byte[] ComputeSHA1Hash(this string s)
#endif
}

#endregion

#region SHA256

/// <summary>
/// Computes the SHA256 hash of the data array.
/// </summary>
Expand Down Expand Up @@ -2575,6 +2565,10 @@ public static byte[] ComputeSHA256Hash(this string s)
#endif
}

#endregion

#region SHA512

/// <summary>
/// Computes the SHA512 hash of the data array.
/// </summary>
Expand Down Expand Up @@ -2632,6 +2626,10 @@ public static byte[] ComputeSHA512Hash(this string s)
#endif
}

#endregion

#region Adler32

/// <summary>
/// Computes a checksum of the data array using the Adler-32 algorithm (<see cref="Adler32"/>).
/// </summary>
Expand Down Expand Up @@ -2687,6 +2685,8 @@ public static uint ComputeAdler32Checksum(this string s)
return a.Checksum;
}

#endregion
#endregion

#endregion
}
}
16 changes: 11 additions & 5 deletions src/Aardvark.Base/Extensions/StringExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -363,16 +363,22 @@ public static IEnumerable<string> NestedBracketSplit(this string text, int split
#region Guid

/// <summary>
/// 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.
/// </summary>
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
}

/// <summary>
/// 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.
/// </summary>
public static Guid ToGuid(this IEnumerable<string> self)
{
Expand All @@ -382,7 +388,7 @@ public static Guid ToGuid(this IEnumerable<string> self)
}

/// <summary>
/// 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.
/// </summary>
public static Guid ToGuid(this IEnumerable<Guid> self)
{
Expand All @@ -391,7 +397,7 @@ public static Guid ToGuid(this IEnumerable<Guid> self)
return sb.ToString().ToGuid();
}

#endregion
#endregion

#region Properties

Expand Down
17 changes: 11 additions & 6 deletions src/Aardvark.Base/Introspection/Introspection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
6 changes: 0 additions & 6 deletions src/Demo/Scratch/Store.fs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 3cf2145

Please sign in to comment.