diff --git a/LibHac/Crypto.cs b/LibHac/Crypto.cs index 115ae84e..711e08e4 100644 --- a/LibHac/Crypto.cs +++ b/LibHac/Crypto.cs @@ -1,5 +1,6 @@ using System; using System.IO; +using System.Linq; using System.Numerics; using System.Security.Cryptography; using LibHac.Streams; @@ -16,7 +17,7 @@ namespace LibHac bool comp = false; using (var _SHA = SHA256.Create()) { - comp = Util.ArraysEqual(hash, _SHA.ComputeHash(data)); + comp = hash.SequenceEqual(_SHA.ComputeHash(data)); } return comp; } @@ -133,7 +134,7 @@ namespace LibHac byte[] testEnc = rsa.Encrypt(test, false); byte[] testDec = rsa.Decrypt(testEnc, false); - if (!Util.ArraysEqual(test, testDec)) + if (!test.SequenceEqual(testDec)) { throw new InvalidDataException("Could not verify RSA key pair"); } diff --git a/LibHac/IntegrityVerificationStream.cs b/LibHac/IntegrityVerificationStream.cs index de6ad6fb..cea494bf 100644 --- a/LibHac/IntegrityVerificationStream.cs +++ b/LibHac/IntegrityVerificationStream.cs @@ -1,5 +1,6 @@ using System; using System.IO; +using System.Linq; using System.Security.Cryptography; using LibHac.Streams; @@ -95,7 +96,7 @@ namespace LibHac hash[0x1F] |= 0x80; } - if (!Util.ArraysEqual(_hashBuffer, hash)) + if (!_hashBuffer.SequenceEqual(hash)) { throw new InvalidDataException("Hash error!"); } diff --git a/LibHac/Keyset.cs b/LibHac/Keyset.cs index c8c15777..7cb77042 100644 --- a/LibHac/Keyset.cs +++ b/LibHac/Keyset.cs @@ -113,7 +113,7 @@ namespace LibHac Array.Copy(EncryptedKeyblobs[i], expectedCmac, 0x10); Crypto.CalculateAesCmac(KeyblobMacKeys[i], EncryptedKeyblobs[i], 0x10, cmac, 0, 0xa0); - if (!Util.ArraysEqual(cmac, expectedCmac)) + if (!cmac.SequenceEqual(expectedCmac)) { logger?.LogMessage($"Warning: Keyblob MAC {i:x2} is invalid. Are SBK/TSEC key correct?"); } diff --git a/LibHac/Nax0.cs b/LibHac/Nax0.cs index b246f67b..0dad1b7d 100644 --- a/LibHac/Nax0.cs +++ b/LibHac/Nax0.cs @@ -1,5 +1,6 @@ using System; using System.IO; +using System.Linq; using System.Security.Cryptography; using System.Text; using LibHac.Streams; @@ -75,7 +76,7 @@ namespace LibHac var validationHash = new HMACSHA256(validationHashKey); byte[] validationMac = validationHash.ComputeHash(keyset.SdCardKeys[k], 0x10, 0x10); - if (Util.ArraysEqual(Hmac, validationMac)) + if (Hmac.SequenceEqual(validationMac)) { return; } diff --git a/LibHac/Nca.cs b/LibHac/Nca.cs index 9675a127..a393798b 100644 --- a/LibHac/Nca.cs +++ b/LibHac/Nca.cs @@ -315,7 +315,7 @@ namespace LibHac actual = hash.ComputeHash(hashTable); } - var validity = Util.ArraysEqual(expected, actual) ? Validity.Valid : Validity.Invalid; + var validity = expected.SequenceEqual(actual) ? Validity.Valid : Validity.Invalid; sect.SuperblockHashValidity = validity; if (sect.Type == SectionType.Romfs) sect.Romfs.IvfcLevels[0].HashValidity = validity; @@ -391,7 +391,7 @@ namespace LibHac section.Read(currentBlock, 0, curBlockSize); var actualHash = sha256.ComputeHash(currentBlock, 0, curBlockSize); - if (!Util.ArraysEqual(expectedHash, actualHash)) + if (!expectedHash.SequenceEqual(actualHash)) { return Validity.Invalid; } diff --git a/LibHac/Savefile/Header.cs b/LibHac/Savefile/Header.cs index 5512a55d..c0aa1ab6 100644 --- a/LibHac/Savefile/Header.cs +++ b/LibHac/Savefile/Header.cs @@ -1,5 +1,6 @@ using System.IO; using System.Security.Cryptography; +using System.Linq; namespace LibHac.Savefile { @@ -91,7 +92,7 @@ namespace LibHac.Savefile using (SHA256 sha256 = SHA256.Create()) { var hash = sha256.ComputeHash(Data, 0x300, 0x3d00); - return Util.ArraysEqual(hash, Layout.Hash) ? Validity.Valid : Validity.Invalid; + return hash.SequenceEqual(Layout.Hash) ? Validity.Valid : Validity.Invalid; } } @@ -101,7 +102,7 @@ namespace LibHac.Savefile Crypto.CalculateAesCmac(keyset.SaveMacKey, Data, 0x100, calculatedCmac, 0, 0x200); - return Util.ArraysEqual(calculatedCmac, Cmac) ? Validity.Valid : Validity.Invalid; + return calculatedCmac.SequenceEqual(Cmac) ? Validity.Valid : Validity.Invalid; } } diff --git a/LibHac/Util.cs b/LibHac/Util.cs index 24557882..de3aae47 100644 --- a/LibHac/Util.cs +++ b/LibHac/Util.cs @@ -30,23 +30,6 @@ namespace LibHac return array; } - public static bool ArraysEqual(T[] a1, T[] a2) - { - if (a1 == null || a2 == null) return false; - if (a1 == a2) return true; - if (a1.Length != a2.Length) return false; - - for (int i = 0; i < a1.Length; i++) - { - if (!a1[i].Equals(a2[i])) - { - return false; - } - } - - return true; - } - public static bool IsEmpty(this byte[] array) { if (array == null) throw new ArgumentNullException(nameof(array));