remove ArraysEqual use SequenceEqual it makes the same

This commit is contained in:
jonnysp 2018-09-27 14:30:01 +02:00
parent 980aca21aa
commit e357c753f5
7 changed files with 13 additions and 26 deletions

View file

@ -1,5 +1,6 @@
using System; using System;
using System.IO; using System.IO;
using System.Linq;
using System.Numerics; using System.Numerics;
using System.Security.Cryptography; using System.Security.Cryptography;
using LibHac.Streams; using LibHac.Streams;
@ -16,7 +17,7 @@ namespace LibHac
bool comp = false; bool comp = false;
using (var _SHA = SHA256.Create()) using (var _SHA = SHA256.Create())
{ {
comp = Util.ArraysEqual(hash, _SHA.ComputeHash(data)); comp = hash.SequenceEqual(_SHA.ComputeHash(data));
} }
return comp; return comp;
} }
@ -133,7 +134,7 @@ namespace LibHac
byte[] testEnc = rsa.Encrypt(test, false); byte[] testEnc = rsa.Encrypt(test, false);
byte[] testDec = rsa.Decrypt(testEnc, 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"); throw new InvalidDataException("Could not verify RSA key pair");
} }

View file

@ -1,5 +1,6 @@
using System; using System;
using System.IO; using System.IO;
using System.Linq;
using System.Security.Cryptography; using System.Security.Cryptography;
using LibHac.Streams; using LibHac.Streams;
@ -95,7 +96,7 @@ namespace LibHac
hash[0x1F] |= 0x80; hash[0x1F] |= 0x80;
} }
if (!Util.ArraysEqual(_hashBuffer, hash)) if (!_hashBuffer.SequenceEqual(hash))
{ {
throw new InvalidDataException("Hash error!"); throw new InvalidDataException("Hash error!");
} }

View file

@ -113,7 +113,7 @@ namespace LibHac
Array.Copy(EncryptedKeyblobs[i], expectedCmac, 0x10); Array.Copy(EncryptedKeyblobs[i], expectedCmac, 0x10);
Crypto.CalculateAesCmac(KeyblobMacKeys[i], EncryptedKeyblobs[i], 0x10, cmac, 0, 0xa0); 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?"); logger?.LogMessage($"Warning: Keyblob MAC {i:x2} is invalid. Are SBK/TSEC key correct?");
} }

View file

@ -1,5 +1,6 @@
using System; using System;
using System.IO; using System.IO;
using System.Linq;
using System.Security.Cryptography; using System.Security.Cryptography;
using System.Text; using System.Text;
using LibHac.Streams; using LibHac.Streams;
@ -75,7 +76,7 @@ namespace LibHac
var validationHash = new HMACSHA256(validationHashKey); var validationHash = new HMACSHA256(validationHashKey);
byte[] validationMac = validationHash.ComputeHash(keyset.SdCardKeys[k], 0x10, 0x10); byte[] validationMac = validationHash.ComputeHash(keyset.SdCardKeys[k], 0x10, 0x10);
if (Util.ArraysEqual(Hmac, validationMac)) if (Hmac.SequenceEqual(validationMac))
{ {
return; return;
} }

View file

@ -315,7 +315,7 @@ namespace LibHac
actual = hash.ComputeHash(hashTable); 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; sect.SuperblockHashValidity = validity;
if (sect.Type == SectionType.Romfs) sect.Romfs.IvfcLevels[0].HashValidity = validity; if (sect.Type == SectionType.Romfs) sect.Romfs.IvfcLevels[0].HashValidity = validity;
@ -391,7 +391,7 @@ namespace LibHac
section.Read(currentBlock, 0, curBlockSize); section.Read(currentBlock, 0, curBlockSize);
var actualHash = sha256.ComputeHash(currentBlock, 0, curBlockSize); var actualHash = sha256.ComputeHash(currentBlock, 0, curBlockSize);
if (!Util.ArraysEqual(expectedHash, actualHash)) if (!expectedHash.SequenceEqual(actualHash))
{ {
return Validity.Invalid; return Validity.Invalid;
} }

View file

@ -1,5 +1,6 @@
using System.IO; using System.IO;
using System.Security.Cryptography; using System.Security.Cryptography;
using System.Linq;
namespace LibHac.Savefile namespace LibHac.Savefile
{ {
@ -91,7 +92,7 @@ namespace LibHac.Savefile
using (SHA256 sha256 = SHA256.Create()) using (SHA256 sha256 = SHA256.Create())
{ {
var hash = sha256.ComputeHash(Data, 0x300, 0x3d00); 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); 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;
} }
} }

View file

@ -30,23 +30,6 @@ namespace LibHac
return array; return array;
} }
public static bool ArraysEqual<T>(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) public static bool IsEmpty(this byte[] array)
{ {
if (array == null) throw new ArgumentNullException(nameof(array)); if (array == null) throw new ArgumentNullException(nameof(array));