Split out a method before crypto step

Allows for capturing the obtaining the raw decrypted data via the PKHeX.Core api
This commit is contained in:
Kurt 2020-01-13 19:03:27 -08:00
parent 2e16fb058c
commit a6aabcdc96

View File

@ -96,11 +96,20 @@ public static bool GetIsHashValid(byte[] data)
/// Hash is assumed to be valid before calling this method.
/// </remarks>
public static IReadOnlyList<SCBlock> Decrypt(byte[] data)
{
var temp = GetDecryptedRawData(data);
return ReadBlocks(temp);
}
/// <summary>
/// Decrypts the save data, with raw block data concatenated together.
/// </summary>
public static byte[] GetDecryptedRawData(byte[] data)
{
// de-ref from input data, since we're going to modify the contents in-place
var temp = (byte[])data.Clone();
CryptStaticXorpadBytes(temp);
return ReadBlocks(temp);
return temp;
}
private static IReadOnlyList<SCBlock> ReadBlocks(byte[] data)
@ -122,6 +131,21 @@ private static IReadOnlyList<SCBlock> ReadBlocks(byte[] data)
/// <param name="blocks">Decrypted save data</param>
/// <returns>Encrypted save data.</returns>
public static byte[] Encrypt(IReadOnlyList<SCBlock> blocks)
{
var result = GetDecryptedRawData(blocks);
CryptStaticXorpadBytes(result);
var hash = ComputeHash(result);
hash.CopyTo(result, result.Length - SIZE_HASH);
return result;
}
/// <summary>
/// Tries to encrypt the save data.
/// </summary>
/// <returns>Raw save data without the final xorpad layer.</returns>
public static byte[] GetDecryptedRawData(IEnumerable<SCBlock> blocks)
{
using var ms = new MemoryStream();
foreach (var block in blocks)
@ -133,12 +157,6 @@ public static byte[] Encrypt(IReadOnlyList<SCBlock> blocks)
// Allocate hash bytes at the end
var result = new byte[ms.Length + SIZE_HASH];
ms.ToArray().CopyTo(result, 0);
CryptStaticXorpadBytes(result);
var hash = ComputeHash(result);
hash.CopyTo(result, result.Length - SIZE_HASH);
return result;
}
}