From 1d3bd2f8b18d5edfb03f839db6a91834004cfc5e Mon Sep 17 00:00:00 2001 From: Matt Nadareski Date: Mon, 4 Mar 2024 21:27:36 -0500 Subject: [PATCH] Use SabreTools.Hashing --- .../BinaryObjectScanner.csproj | 1 + .../Protection/Macrovision.RipGuard.cs | 4 +- .../Protection/Macrovision.SafeDisc.cs | 12 ++--- BinaryObjectScanner/Utilities/Hashing.cs | 53 ------------------- 4 files changed, 9 insertions(+), 61 deletions(-) delete mode 100644 BinaryObjectScanner/Utilities/Hashing.cs diff --git a/BinaryObjectScanner/BinaryObjectScanner.csproj b/BinaryObjectScanner/BinaryObjectScanner.csproj index c67729d0..fa4dff93 100644 --- a/BinaryObjectScanner/BinaryObjectScanner.csproj +++ b/BinaryObjectScanner/BinaryObjectScanner.csproj @@ -84,6 +84,7 @@ + diff --git a/BinaryObjectScanner/Protection/Macrovision.RipGuard.cs b/BinaryObjectScanner/Protection/Macrovision.RipGuard.cs index 8d75a14d..a202d8f5 100644 --- a/BinaryObjectScanner/Protection/Macrovision.RipGuard.cs +++ b/BinaryObjectScanner/Protection/Macrovision.RipGuard.cs @@ -4,9 +4,9 @@ #endif using System.Collections.Generic; using System.IO; +using SabreTools.Hashing; using SabreTools.Matching; using SabreTools.Serialization.Wrappers; -using static BinaryObjectScanner.Utilities.Hashing; namespace BinaryObjectScanner.Protection { @@ -47,7 +47,7 @@ public partial class Macrovision // So far, every seemingly-randomly named EXE on RipGuard discs have a consistent hash. if (fi.Length == 49_152) { - var sha1 = GetFileSHA1(file); + var sha1 = HashTool.GetFileHash(file, HashType.SHA1); if (sha1 == "6A7B8545800E0AB252773A8CD0A2185CA2497938") return "RipGuard"; } diff --git a/BinaryObjectScanner/Protection/Macrovision.SafeDisc.cs b/BinaryObjectScanner/Protection/Macrovision.SafeDisc.cs index f0dbf915..401f90fc 100644 --- a/BinaryObjectScanner/Protection/Macrovision.SafeDisc.cs +++ b/BinaryObjectScanner/Protection/Macrovision.SafeDisc.cs @@ -5,9 +5,9 @@ using System.Collections.Generic; using System.IO; using System.Linq; +using SabreTools.Hashing; using SabreTools.Matching; using SabreTools.Serialization.Wrappers; -using static BinaryObjectScanner.Utilities.Hashing; namespace BinaryObjectScanner.Protection { @@ -363,7 +363,7 @@ internal static string GetSafeDiscCLCD16Version(string firstMatchedString, IEnum return string.Empty; // The hash of the file CLCD16.dll is able to provide a broad version range that appears to be consistent, but it seems it was rarely updated so these checks are quite broad. - var sha1 = GetFileSHA1(firstMatchedString); + var sha1 = HashTool.GetFileHash(firstMatchedString, HashType.SHA1); return sha1 switch { // Found in Redump entries 61731 and 66005. @@ -386,7 +386,7 @@ internal static string GetSafeDiscCLCD32Version(string firstMatchedString, IEnum return string.Empty; // The hash of the file CLCD32.dll so far appears to be a solid indicator of version for versions it was used with. It appears to have been updated with every release, unlike its counterpart, CLCD16.dll. - var sha1 = GetFileSHA1(firstMatchedString); + var sha1 = HashTool.GetFileHash(firstMatchedString, HashType.SHA1); return sha1 switch { // Found in Redump entry 66005. @@ -485,7 +485,7 @@ internal static string GetSafeDiscCLOKSPLVersion(string firstMatchedString, IEnu // The hash of every "CLOKSPL.EXE" correlates directly to a specific SafeDisc version. - var sha1 = GetFileSHA1(firstMatchedString); + var sha1 = HashTool.GetFileHash(firstMatchedString, HashType.SHA1); return sha1 switch { // Found in Redump entry 66005. @@ -632,7 +632,7 @@ internal static string GetSafeDiscDrvmgtVersion(string firstMatchedString, IEnum // There are occasionaly inconsistencies, even within the well detected version range. This seems to me to mostly happen with later (3.20+) games, and seems to me to be an example of the SafeDisc distribution becoming more disorganized with time. // Particularly interesting inconsistencies will be noted below: // Redump entry 73786 has an EXE with a scrubbed version, a DIAG.exe with a version of 4.60.000, and a copy of drvmgt.dll belonging to version 3.10.020. This seems like an accidental(?) distribution of older drivers, as this game was released 3 years after the use of 3.10.020. - var sha1 = GetFileSHA1(firstMatchedString); + var sha1 = HashTool.GetFileHash(firstMatchedString, HashType.SHA1); return sha1 switch { // Found in Redump entry 102979. @@ -780,7 +780,7 @@ internal static string GetSafeDiscDrvmgtVersion(string firstMatchedString, IEnum if (string.IsNullOrEmpty(firstMatchedString) || !File.Exists(firstMatchedString)) return string.Empty; - var sha1 = GetFileSHA1(firstMatchedString); + var sha1 = HashTool.GetFileHash(firstMatchedString, HashType.SHA1); switch (sha1) { // Found in Redump entry 63488. diff --git a/BinaryObjectScanner/Utilities/Hashing.cs b/BinaryObjectScanner/Utilities/Hashing.cs deleted file mode 100644 index 739a7163..00000000 --- a/BinaryObjectScanner/Utilities/Hashing.cs +++ /dev/null @@ -1,53 +0,0 @@ -using System; -using System.IO; -using System.Security.Cryptography; - -namespace BinaryObjectScanner.Utilities -{ - /// - /// Data hashing methods - /// - public static class Hashing - { - /// - /// Get the SHA1 hash of a file, if possible - /// - /// Path to the file to be hashed - /// SHA1 hash as a string on success, null on error - public static string? GetFileSHA1(string? path) - { - if (string.IsNullOrEmpty(path)) - return null; - - try - { - var sha1 = SHA1.Create(); - using (Stream fileStream = File.Open(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) - { - byte[] buffer = new byte[32768]; - while (true) - { - int bytesRead = fileStream.Read(buffer, 0, 32768); - if (bytesRead == 32768) - { - sha1.TransformBlock(buffer, 0, bytesRead, null, 0); - } - else - { - sha1.TransformFinalBlock(buffer, 0, bytesRead); - break; - } - } - } - - string hash = BitConverter.ToString(sha1.Hash!); - hash = hash.Replace("-", string.Empty); - return hash; - } - catch - { - return null; - } - } - } -}