diff --git a/src/BizHawk.Common/checksums/CRC32Checksum.cs b/src/BizHawk.Common/checksums/CRC32Checksum.cs new file mode 100644 index 0000000000..a988b4b803 --- /dev/null +++ b/src/BizHawk.Common/checksums/CRC32Checksum.cs @@ -0,0 +1,33 @@ +using System; + +using BizHawk.Common.BufferExtensions; + +namespace BizHawk.Common +{ + /// uses custom implementation of CRC-32 (i.e. POSIX cksum) + /// + /// + /// + public static class CRC32Checksum + { + /// in bits + internal const int EXPECTED_LENGTH = 32; + + internal const string PREFIX = "CRC32"; + + public static byte[] BytesAsDigest(uint digest) + { + var a = BitConverter.GetBytes(digest); + return new[] { a[3], a[2], a[1], a[0] }; + } + + public static byte[] Compute(ReadOnlySpan data) + => BytesAsDigest(CRC32.Calculate(data)); + + public static string ComputeDigestHex(ReadOnlySpan data) + => Compute(data).BytesToHexString(); + + public static string ComputePrefixedHex(ReadOnlySpan data) + => $"{PREFIX}:{ComputeDigestHex(data)}"; + } +} diff --git a/src/BizHawk.Common/checksums/MD5Checksum.cs b/src/BizHawk.Common/checksums/MD5Checksum.cs index bff4627401..5ccfcf817e 100644 --- a/src/BizHawk.Common/checksums/MD5Checksum.cs +++ b/src/BizHawk.Common/checksums/MD5Checksum.cs @@ -7,6 +7,7 @@ using BizHawk.Common.BufferExtensions; namespace BizHawk.Common { /// uses implementation from BCL + /// /// /// public static class MD5Checksum diff --git a/src/BizHawk.Common/checksums/SHA1Checksum.cs b/src/BizHawk.Common/checksums/SHA1Checksum.cs index b9bb59a428..671dbf00c4 100644 --- a/src/BizHawk.Common/checksums/SHA1Checksum.cs +++ b/src/BizHawk.Common/checksums/SHA1Checksum.cs @@ -7,6 +7,7 @@ using BizHawk.Common.BufferExtensions; namespace BizHawk.Common { /// uses implementation from BCL + /// /// /// public static class SHA1Checksum diff --git a/src/BizHawk.Common/checksums/SHA256Checksum.cs b/src/BizHawk.Common/checksums/SHA256Checksum.cs index ab0fb2904c..63199db60d 100644 --- a/src/BizHawk.Common/checksums/SHA256Checksum.cs +++ b/src/BizHawk.Common/checksums/SHA256Checksum.cs @@ -7,6 +7,7 @@ using BizHawk.Common.BufferExtensions; namespace BizHawk.Common { /// uses implementation from BCL + /// /// /// public static class SHA256Checksum diff --git a/src/BizHawk.Emulation.Common/Database/Database.cs b/src/BizHawk.Emulation.Common/Database/Database.cs index f7ea6f2f40..18a2d40ae5 100644 --- a/src/BizHawk.Emulation.Common/Database/Database.cs +++ b/src/BizHawk.Emulation.Common/Database/Database.cs @@ -220,7 +220,7 @@ namespace BizHawk.Emulation.Common { acquire.WaitOne(); - var hashCRC32 = CRC32.Calculate(romData).ToString("X8"); + var hashCRC32 = CRC32Checksum.ComputeDigestHex(romData); if (DB.TryGetValue(hashCRC32, out var cgi)) { return new GameInfo(cgi); diff --git a/src/BizHawk.Emulation.DiscSystem/DiscHasher.cs b/src/BizHawk.Emulation.DiscSystem/DiscHasher.cs index 39bceb3621..b0657d252f 100644 --- a/src/BizHawk.Emulation.DiscSystem/DiscHasher.cs +++ b/src/BizHawk.Emulation.DiscSystem/DiscHasher.cs @@ -54,7 +54,7 @@ namespace BizHawk.Emulation.DiscSystem crc.Add(buffer2352); } - return crc.Result.ToString("X8"); + return CRC32Checksum.BytesAsDigest(crc.Result).BytesToHexString(); } /// @@ -102,4 +102,4 @@ namespace BizHawk.Emulation.DiscSystem return "no data track found"; } } -} +} \ No newline at end of file