Revert commit [4b9ef5d]. We need that kind of hashing.

This commit is contained in:
Maschell 2019-04-30 15:43:37 +02:00
parent b432537af7
commit 62b87fe925

View File

@ -30,6 +30,7 @@ import de.mas.wiiu.jnus.entities.content.Content;
import de.mas.wiiu.jnus.utils.ByteArrayBuffer; import de.mas.wiiu.jnus.utils.ByteArrayBuffer;
import de.mas.wiiu.jnus.utils.CheckSumWrongException; import de.mas.wiiu.jnus.utils.CheckSumWrongException;
import de.mas.wiiu.jnus.utils.HashUtil; import de.mas.wiiu.jnus.utils.HashUtil;
import de.mas.wiiu.jnus.utils.PipedInputStreamWithException;
import de.mas.wiiu.jnus.utils.StreamUtils; import de.mas.wiiu.jnus.utils.StreamUtils;
import de.mas.wiiu.jnus.utils.Utils; import de.mas.wiiu.jnus.utils.Utils;
import lombok.extern.java.Log; import lombok.extern.java.Log;
@ -59,10 +60,12 @@ public class NUSDecryption extends AESDecryption {
public void decryptFileStream(InputStream inputStream, OutputStream outputStream, long fileOffset, long filesize, byte[] IV, byte[] h3hash, public void decryptFileStream(InputStream inputStream, OutputStream outputStream, long fileOffset, long filesize, byte[] IV, byte[] h3hash,
long expectedSizeForHash) throws IOException, CheckSumWrongException { long expectedSizeForHash) throws IOException, CheckSumWrongException {
MessageDigest sha1 = null; MessageDigest sha1 = null;
MessageDigest sha1fallback = null;
if (h3hash != null) { if (h3hash != null) {
try { try {
sha1 = MessageDigest.getInstance("SHA1"); sha1 = MessageDigest.getInstance("SHA1");
sha1fallback = MessageDigest.getInstance("SHA1");
} catch (NoSuchAlgorithmException e) { } catch (NoSuchAlgorithmException e) {
e.printStackTrace(); e.printStackTrace();
} }
@ -74,7 +77,7 @@ public class NUSDecryption extends AESDecryption {
int inBlockBuffer; int inBlockBuffer;
long written = 0; long written = 0;
long writtenHash = 0; long writtenFallback = 0;
try { try {
ByteArrayBuffer overflow = new ByteArrayBuffer(BLOCKSIZE); ByteArrayBuffer overflow = new ByteArrayBuffer(BLOCKSIZE);
@ -108,33 +111,39 @@ public class NUSDecryption extends AESDecryption {
outputStream.write(output, 0, toWrite); outputStream.write(output, 0, toWrite);
if (sha1 != null) { if (sha1 != null && sha1fallback != null) {
sha1.update(output, 0, toWrite);
// In some cases it's using the hash of the whole .app file instead of the part // In some cases it's using the hash of the whole .app file instead of the part
// that's been actually used. // that's been actually used.
long toFallback = inBlockBuffer; long toFallback = inBlockBuffer;
if (written + toFallback > expectedSizeForHash) { if (writtenFallback + toFallback > expectedSizeForHash) {
toFallback = expectedSizeForHash - written; toFallback = expectedSizeForHash - writtenFallback;
} }
sha1.update(output, 0, (int) toFallback); sha1fallback.update(output, 0, (int) toFallback);
writtenHash += toFallback; writtenFallback += toFallback;
} }
if (written >= filesize && h3hash == null) { if (written >= filesize && h3hash == null) {
break; break;
} }
} while (inBlockBuffer == BLOCKSIZE); } while (inBlockBuffer == BLOCKSIZE);
if (sha1 != null) { if (sha1 != null && sha1fallback != null) {
long missingInHash = expectedSizeForHash - writtenHash; long missingInHash = expectedSizeForHash - writtenFallback;
if (missingInHash > 0) { if (missingInHash > 0) {
sha1.update(new byte[(int) missingInHash]); sha1fallback.update(new byte[(int) missingInHash]);
} }
byte[] calculated_hash1 = sha1.digest(); byte[] calculated_hash1 = sha1.digest();
if (!Arrays.equals(calculated_hash1, h3hash)) { byte[] calculated_hash2 = sha1fallback.digest();
throw new CheckSumWrongException("hash checksum failed", calculated_hash1, h3hash); byte[] expected_hash = h3hash;
if (!Arrays.equals(calculated_hash1, expected_hash) && !Arrays.equals(calculated_hash2, expected_hash)) {
throw new CheckSumWrongException("hash checksum failed ", calculated_hash1, expected_hash);
} else { } else {
log.finest("Hash DOES match saves output stream."); log.finest("Hash DOES match saves output stream.");
} }
} }
} finally { } finally {
StreamUtils.closeAll(inputStream, outputStream); StreamUtils.closeAll(inputStream, outputStream);
@ -243,7 +252,7 @@ public class NUSDecryption extends AESDecryption {
} finally { } finally {
StreamUtils.closeAll(inputStream, outputStream); StreamUtils.closeAll(inputStream, outputStream);
} }
return true; return true;
} }