From 659b408a593aa48fecdd5aeb71ed5f2dc2f133aa Mon Sep 17 00:00:00 2001 From: Maschell Date: Thu, 6 Dec 2018 15:29:57 +0100 Subject: [PATCH] Fix the hash calculation for some files in "non hashed" .app files --- .../jnus/utils/cryptography/NUSDecryption.java | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/src/de/mas/wiiu/jnus/utils/cryptography/NUSDecryption.java b/src/de/mas/wiiu/jnus/utils/cryptography/NUSDecryption.java index bb06de4..2cc5acf 100644 --- a/src/de/mas/wiiu/jnus/utils/cryptography/NUSDecryption.java +++ b/src/de/mas/wiiu/jnus/utils/cryptography/NUSDecryption.java @@ -80,6 +80,7 @@ public class NUSDecryption extends AESDecryption { int inBlockBuffer; long written = 0; + long writtenFallback = 0; ByteArrayBuffer overflow = new ByteArrayBuffer(BLOCKSIZE); @@ -101,12 +102,21 @@ public class NUSDecryption extends AESDecryption { if (sha1 != null && sha1fallback != null) { sha1.update(output, 0, toWrite); - sha1fallback.update(output, 0, toWrite); + + // In some cases it's using the hash of the whole .app file instead of the part + // that's been actually used. + long toFallback = inBlockBuffer; + if (writtenFallback + toFallback > expectedSizeForHash) { + toFallback = expectedSizeForHash - writtenFallback; + } + sha1fallback.update(output, 0, (int) toFallback); + writtenFallback += toFallback; } } while (inBlockBuffer == BLOCKSIZE); if (sha1 != null && sha1fallback != null) { - long missingInHash = expectedSizeForHash - written; + + long missingInHash = expectedSizeForHash - writtenFallback; if (missingInHash > 0) { sha1fallback.update(new byte[(int) missingInHash]); } @@ -118,6 +128,7 @@ public class NUSDecryption extends AESDecryption { outputStream.close(); inputStream.close(); throw new CheckSumWrongException("hash checksum failed", calculated_hash1, expected_hash); + } else { // log.warning("Hash DOES match saves output stream."); }