Handle Terra card header hashes in XciHeader

This commit is contained in:
Alex Barney 2024-05-27 21:26:44 -07:00
parent f0477a87dc
commit a8bb53fc68

View file

@ -10,6 +10,7 @@ using LibHac.Fs;
using LibHac.Gc.Impl; using LibHac.Gc.Impl;
using LibHac.Tools.Crypto; using LibHac.Tools.Crypto;
using LibHac.Tools.FsSystem; using LibHac.Tools.FsSystem;
using LibHac.Util;
using Aes = LibHac.Crypto.Aes; using Aes = LibHac.Crypto.Aes;
namespace LibHac.Tools.Fs; namespace LibHac.Tools.Fs;
@ -177,9 +178,19 @@ public class XciHeader
reader.BaseStream.Position = RootPartitionOffset; reader.BaseStream.Position = RootPartitionOffset;
byte[] headerBytes = reader.ReadBytes((int)RootPartitionHeaderSize); byte[] headerBytes = reader.ReadBytes((int)RootPartitionHeaderSize);
Span<byte> actualHeaderHash = stackalloc byte[Sha256.DigestSize]; Span<byte> actualHeaderHash = stackalloc byte[Sha256.DigestSize];
Sha256.GenerateSha256Hash(headerBytes, actualHeaderHash);
Optional<byte> salt = CompatibilityType == 0 ? new Optional<byte>() : 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; PartitionFsHeaderValidity = Utilities.SpansEqual(RootPartitionHeaderHash, actualHeaderHash) ? Validity.Valid : Validity.Invalid;