mirror of
https://github.com/Maschell/JNUSTool.git
synced 2024-06-02 15:38:47 +02:00
f03719487f
- Added serval hash checks (h0-h3 + h3 file hash) - If the decrypted file already exists, but the hash is missing it'll re downloaded. - to download the meta.xml the beginning the cached files will be used if they exist. - improved content type detection (fixed issue with some wrong decrypted files in the code folder) - added an automatic updatelist.csv updater - refactoring / code cleanup / several small fixes I don't remember note: make sure to use the new config file. It contains the URL's for the auto update feature
89 lines
3.0 KiB
Java
89 lines
3.0 KiB
Java
package de.mas.jnustool.util;
|
|
|
|
import java.io.File;
|
|
import java.io.FileInputStream;
|
|
import java.io.FileNotFoundException;
|
|
import java.io.IOException;
|
|
import java.io.InputStream;
|
|
import java.nio.ByteBuffer;
|
|
import java.security.MessageDigest;
|
|
import java.security.NoSuchAlgorithmException;
|
|
import java.util.Arrays;
|
|
|
|
public class HashUtil {
|
|
public static byte[] hashSHA2(byte[] data){
|
|
MessageDigest sha256;
|
|
try {
|
|
sha256 = MessageDigest.getInstance("SHA-256");
|
|
} catch (NoSuchAlgorithmException e) {
|
|
e.printStackTrace();
|
|
return new byte[0x20];
|
|
}
|
|
|
|
return sha256.digest(data);
|
|
}
|
|
|
|
public static byte[] hashSHA1(byte[] data){
|
|
MessageDigest sha1;
|
|
try {
|
|
sha1 = MessageDigest.getInstance("SHA1");
|
|
} catch (NoSuchAlgorithmException e) {
|
|
e.printStackTrace();
|
|
return new byte[0x14];
|
|
}
|
|
|
|
return sha1.digest(data);
|
|
}
|
|
|
|
public static byte[] hashSHA1(File file) {
|
|
return hashSHA1(file, 0);
|
|
}
|
|
|
|
public static byte[] hashSHA1(File file,int aligmnent) {
|
|
byte[] hash = new byte[0x14];
|
|
MessageDigest sha1 = null;
|
|
try {
|
|
InputStream in = new FileInputStream(file);
|
|
sha1 = MessageDigest.getInstance("SHA1");
|
|
hash = hash(sha1,in,file.length(),0x8000,aligmnent);
|
|
} catch (NoSuchAlgorithmException | FileNotFoundException e) {
|
|
e.printStackTrace();
|
|
} catch (IOException e) {
|
|
e.printStackTrace();
|
|
}
|
|
|
|
return hash;
|
|
}
|
|
|
|
public static byte [] hash(MessageDigest digest, InputStream in,long inputSize, int bufferSize,int alignment) throws IOException {
|
|
long target_size = alignment == 0 ? inputSize: Util.align(inputSize, alignment);
|
|
long cur_position = 0;
|
|
int inBlockBufferRead = 0;
|
|
byte[] blockBuffer = new byte[bufferSize];
|
|
ByteArrayBuffer overflow = new ByteArrayBuffer(bufferSize);
|
|
do{
|
|
if(cur_position + bufferSize > inputSize){
|
|
int expectedSize = (int) (inputSize - cur_position);
|
|
ByteBuffer buffer = ByteBuffer.allocate(bufferSize);
|
|
|
|
inBlockBufferRead = Util.getChunkFromStream(in,blockBuffer,overflow,expectedSize);
|
|
buffer.put(Arrays.copyOfRange(blockBuffer, 0, inBlockBufferRead));
|
|
blockBuffer = buffer.array();
|
|
inBlockBufferRead = bufferSize;
|
|
}else{
|
|
int expectedSize = bufferSize;
|
|
inBlockBufferRead = Util.getChunkFromStream(in,blockBuffer,overflow,expectedSize);
|
|
}
|
|
digest.update(blockBuffer, 0, inBlockBufferRead);
|
|
cur_position += inBlockBufferRead;
|
|
|
|
}while(cur_position < target_size && (inBlockBufferRead == bufferSize));
|
|
|
|
in.close();
|
|
|
|
return digest.digest();
|
|
}
|
|
|
|
|
|
}
|