From b4b1310934cf93fb03d79dc2b66a97632dfd7c03 Mon Sep 17 00:00:00 2001 From: Kurt Date: Sun, 26 Feb 2023 15:18:32 -0800 Subject: [PATCH] Rework jagged crypt operation Fixes checksums for Colo Closes #3803 --- PKHeX.Core/Saves/SAV3Colosseum.cs | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/PKHeX.Core/Saves/SAV3Colosseum.cs b/PKHeX.Core/Saves/SAV3Colosseum.cs index d4b35c406..6a8e94c4e 100644 --- a/PKHeX.Core/Saves/SAV3Colosseum.cs +++ b/PKHeX.Core/Saves/SAV3Colosseum.cs @@ -165,8 +165,10 @@ private static byte[] EncryptColosseum(ReadOnlySpan input, Span dige for (int i = 0; i < digest.Length; i++) digest[i] = (byte)~digest[i]; - var crypt = output.AsSpan(0x18, SLOT_SIZE - (2 * sha1HashSize)); - for (int i = 0; i < crypt.Length; i += sha1HashSize) + const int start = 0x18; + const int end = (SLOT_SIZE - (2 * sha1HashSize)); + var crypt = output.AsSpan(); + for (int i = start; i < end; i += sha1HashSize) { var slice = crypt.Slice(i, digest.Length); for (int j = 0; j < digest.Length; j++) @@ -188,12 +190,14 @@ private static byte[] DecryptColosseum(ReadOnlySpan input, Span dige digest[i] = (byte)~digest[i]; Span hash = stackalloc byte[sha1HashSize]; - var crypt = output.AsSpan(0x18, SLOT_SIZE - (2 * sha1HashSize)); - for (int i = 0; i < crypt.Length; i += sha1HashSize) + const int start = 0x18; + const int end = (SLOT_SIZE - (2 * sha1HashSize)); + var crypt = output.AsSpan(); + for (int i = start; i < end; i += sha1HashSize) { - var slice = crypt.Slice(i, sha1HashSize); + var slice = crypt.Slice(i, Math.Min(crypt.Length - i, sha1HashSize)); SHA1.HashData(slice, hash); // update digest - for (int j = 0; j < digest.Length; j++) + for (int j = 0; j < slice.Length; j++) slice[j] ^= digest[j]; hash.CopyTo(digest); // for use in next loop }