ConditionalHashEquals is fun

This commit is contained in:
Matt Nadareski
2023-08-14 18:43:56 -04:00
parent 5b89d382a7
commit 9a41d16e58
5 changed files with 55 additions and 56 deletions

View File

@@ -1,4 +1,6 @@
using System.IO;
using System;
using System.IO;
using System.Linq;
namespace SabreTools.Core.Tools
{
@@ -7,6 +9,40 @@ namespace SabreTools.Core.Tools
/// </summary>
public static class Utilities
{
/// <summary>
/// Determine if two hashes are equal for the purposes of merging
/// </summary>
public static bool ConditionalHashEquals(byte[]? firstHash, byte[]? secondHash)
{
// If either hash is empty, we say they're equal for merging
if (firstHash.IsNullOrEmpty() || secondHash.IsNullOrEmpty())
return true;
// If they're different sizes, they can't match
if (firstHash!.Length != secondHash!.Length)
return false;
// Otherwise, they need to match exactly
return Enumerable.SequenceEqual(firstHash, secondHash);
}
/// <summary>
/// Determine if two hashes are equal for the purposes of merging
/// </summary>
public static bool ConditionalHashEquals(string? firstHash, string? secondHash)
{
// If either hash is empty, we say they're equal for merging
if (string.IsNullOrWhiteSpace(firstHash) || string.IsNullOrWhiteSpace(secondHash))
return true;
// If they're different sizes, they can't match
if (firstHash!.Length != secondHash!.Length)
return false;
// Otherwise, they need to match exactly
return string.Equals(firstHash, secondHash, StringComparison.OrdinalIgnoreCase);
}
/// <summary>
/// Get a proper romba sub path
/// </summary>