From a8bb53fc68c5e210ff6a6a21d52ca769dcfebc90 Mon Sep 17 00:00:00 2001 From: Alex Barney Date: Mon, 27 May 2024 21:26:44 -0700 Subject: [PATCH] Handle Terra card header hashes in XciHeader --- src/LibHac/Tools/Fs/XciHeader.cs | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/src/LibHac/Tools/Fs/XciHeader.cs b/src/LibHac/Tools/Fs/XciHeader.cs index aaa22db8..915dfd04 100644 --- a/src/LibHac/Tools/Fs/XciHeader.cs +++ b/src/LibHac/Tools/Fs/XciHeader.cs @@ -10,6 +10,7 @@ using LibHac.Fs; using LibHac.Gc.Impl; using LibHac.Tools.Crypto; using LibHac.Tools.FsSystem; +using LibHac.Util; using Aes = LibHac.Crypto.Aes; namespace LibHac.Tools.Fs; @@ -177,9 +178,19 @@ public class XciHeader reader.BaseStream.Position = RootPartitionOffset; byte[] headerBytes = reader.ReadBytes((int)RootPartitionHeaderSize); - Span actualHeaderHash = stackalloc byte[Sha256.DigestSize]; - Sha256.GenerateSha256Hash(headerBytes, actualHeaderHash); + + Optional salt = CompatibilityType == 0 ? new Optional() : CompatibilityType; + + var generator = new Sha256Generator(); + generator.Initialize(); + generator.Update(headerBytes); + if (salt.HasValue) + { + generator.Update(SpanHelpers.AsReadOnlyByteSpan(in salt.ValueRo)); + } + + generator.GetHash(actualHeaderHash); PartitionFsHeaderValidity = Utilities.SpansEqual(RootPartitionHeaderHash, actualHeaderHash) ? Validity.Valid : Validity.Invalid;