diff --git a/LibHac/Crypto.cs b/LibHac/Crypto.cs index 711e08e4..209e1a74 100644 --- a/LibHac/Crypto.cs +++ b/LibHac/Crypto.cs @@ -12,12 +12,12 @@ namespace LibHac internal const int Aes128Size = 0x10; internal const int Sha256DigestSize = 0x20; - public static bool CheckMemoryHashTable(byte[] data, byte[] hash) + public static Validity CheckMemoryHashTable(byte[] data, byte[] hash, int offset , int count) { - bool comp = false; + Validity comp = Validity.Invalid ; using (var _SHA = SHA256.Create()) { - comp = hash.SequenceEqual(_SHA.ComputeHash(data)); + comp = hash.SequenceEqual(_SHA.ComputeHash(data , offset , count)) ? Validity.Valid : Validity.Invalid; } return comp; } diff --git a/LibHac/Pfs.cs b/LibHac/Pfs.cs index 90dfda57..323bac57 100644 --- a/LibHac/Pfs.cs +++ b/LibHac/Pfs.cs @@ -141,14 +141,7 @@ namespace LibHac for (int i = 0; i < NumFiles; i++) { reader.BaseStream.Position = HeaderSize + Files[i].Offset; - if (Crypto.CheckMemoryHashTable(reader.ReadBytes((int)Files[i].HashedRegionSize), Files[i].Hash)) - { - Files[i].HashValidity = Validity.Valid; - } - else - { - Files[i].HashValidity = Validity.Invalid; - } + Files[i].HashValidity = Crypto.CheckMemoryHashTable(reader.ReadBytes(Files[i].HashedRegionSize), Files[i].Hash, 0, Files[i].HashedRegionSize); } } diff --git a/LibHac/Savefile/Header.cs b/LibHac/Savefile/Header.cs index c0aa1ab6..5d5f9e82 100644 --- a/LibHac/Savefile/Header.cs +++ b/LibHac/Savefile/Header.cs @@ -1,5 +1,4 @@ using System.IO; -using System.Security.Cryptography; using System.Linq; namespace LibHac.Savefile @@ -81,21 +80,12 @@ namespace LibHac.Savefile MetaMapEntries[i] = new MapEntry(reader); } - HeaderHashValidity = ValidateHeaderHash(); + HeaderHashValidity = Crypto.CheckMemoryHashTable(Data, Layout.Hash, 0x300, 0x3d00); SignatureValidity = ValidateSignature(keyset); logger?.LogMessage($"Header hash is {HeaderHashValidity}"); } - private Validity ValidateHeaderHash() - { - using (SHA256 sha256 = SHA256.Create()) - { - var hash = sha256.ComputeHash(Data, 0x300, 0x3d00); - return hash.SequenceEqual(Layout.Hash) ? Validity.Valid : Validity.Invalid; - } - } - private Validity ValidateSignature(Keyset keyset) { var calculatedCmac = new byte[0x10]; diff --git a/LibHac/XciHeader.cs b/LibHac/XciHeader.cs index a6d367e4..91481407 100644 --- a/LibHac/XciHeader.cs +++ b/LibHac/XciHeader.cs @@ -134,14 +134,7 @@ namespace LibHac } reader.BaseStream.Position = PartitionFsHeaderAddress; - - if (Crypto.CheckMemoryHashTable(reader.ReadBytes((int)PartitionFsHeaderSize), PartitionFsHeaderHash)) { - PartitionFsHeaderValidity = Validity.Valid; - } - else - { - PartitionFsHeaderValidity = Validity.Invalid; - } + PartitionFsHeaderValidity = Crypto.CheckMemoryHashTable(reader.ReadBytes((int)PartitionFsHeaderSize), PartitionFsHeaderHash, 0, (int)PartitionFsHeaderSize); }