Move back to internal sha256 hashing

This commit is contained in:
rtm516 2020-06-03 01:40:17 +01:00
parent f93b07491e
commit 812a3d82b2
2 changed files with 21 additions and 27 deletions

View File

@ -36,6 +36,8 @@ import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.function.Function;
public class FileUtils {
@ -59,7 +61,7 @@ public class FileUtils {
}
public static <T> T loadJson(InputStream src, Class<T> valueType) throws IOException {
ObjectMapper objectMapper = new ObjectMapper(new JsonFactory()).enable(JsonParser.Feature.IGNORE_UNDEFINED).disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
ObjectMapper objectMapper = new ObjectMapper(new JsonFactory()).enable(JsonParser.Feature.IGNORE_UNDEFINED).enable(JsonParser.Feature.ALLOW_COMMENTS).disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
return objectMapper.readValue(src, valueType);
}
@ -152,4 +154,21 @@ public class FileUtils {
}
return stream;
}
/**
* Calculate the SHA256 hash of the resource pack file
* @param file File to calculate the hash for
* @return A byte[] representation of the hash
*/
public static byte[] calculateSHA256(File file) {
byte[] sha256;
try {
sha256 = MessageDigest.getInstance("SHA-256").digest(Files.readAllBytes(file.toPath()));
} catch (Exception e) {
throw new RuntimeException("Could not calculate pack hash", e);
}
return sha256;
}
}

View File

@ -1,20 +1,14 @@
package org.geysermc.connector.utils;
import com.voxelwind.server.jni.hash.JavaHash;
import com.voxelwind.server.jni.hash.NativeHash;
import com.voxelwind.server.jni.hash.VoxelwindHash;
import net.md_5.bungee.jni.NativeCode;
import org.geysermc.connector.GeyserConnector;
import java.io.File;
import java.nio.file.Files;
import java.util.HashMap;
import java.util.Map;
import java.util.zip.ZipFile;
public class ResourcePack {
public static final Map<String, ResourcePack> PACKS = new HashMap<>();
public static final NativeCode<VoxelwindHash> HASH = new NativeCode<>("native-hash", JavaHash.class, NativeHash.class);
public static final int CHUNK_SIZE = 102400;
private byte[] sha256;
@ -23,14 +17,6 @@ public class ResourcePack {
private ResourcePackManifest.Version version;
public static void loadPacks() {
Map<String, String> hashes = new HashMap<>();
try {
Files.lines(new File("packs/hashes.txt").toPath()).forEach((x) -> hashes.put(x.split("=")[0], x.split("=")[1]));
} catch (Exception e) {
//
}
File directory = new File("packs");
if (!directory.exists()) {
@ -41,7 +27,7 @@ public class ResourcePack {
if(file.getName().endsWith(".zip") || file.getName().endsWith(".mcpack")) {
ResourcePack pack = new ResourcePack();
pack.sha256 = getBytes(hashes.get(file.getName()));
pack.sha256 = FileUtils.calculateSHA256(file);
try {
ZipFile zip = new ZipFile(file);
@ -69,17 +55,6 @@ public class ResourcePack {
}
}
private static byte[] getBytes(String string) {
String[] strings = string.replace("]", "").replace("[", "").replaceAll(" ", "").split(",");
byte[] bytes = new byte[strings.length];
for(int i = 0; i < strings.length; i++) {
bytes[i] = Byte.parseByte(strings[i]);
}
return bytes;
}
public byte[] getSha256() {
return sha256;
}