From 812a3d82b2a4d34e67bbccc193fbd87ac9fb174b Mon Sep 17 00:00:00 2001 From: rtm516 Date: Wed, 3 Jun 2020 01:40:17 +0100 Subject: [PATCH] Move back to internal sha256 hashing --- .../geysermc/connector/utils/FileUtils.java | 21 ++++++++++++++- .../connector/utils/ResourcePack.java | 27 +------------------ 2 files changed, 21 insertions(+), 27 deletions(-) diff --git a/connector/src/main/java/org/geysermc/connector/utils/FileUtils.java b/connector/src/main/java/org/geysermc/connector/utils/FileUtils.java index aedfb8406..dc7a41a48 100644 --- a/connector/src/main/java/org/geysermc/connector/utils/FileUtils.java +++ b/connector/src/main/java/org/geysermc/connector/utils/FileUtils.java @@ -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 loadJson(InputStream src, Class 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; + } } diff --git a/connector/src/main/java/org/geysermc/connector/utils/ResourcePack.java b/connector/src/main/java/org/geysermc/connector/utils/ResourcePack.java index 70b1d8adc..f2b5d166b 100644 --- a/connector/src/main/java/org/geysermc/connector/utils/ResourcePack.java +++ b/connector/src/main/java/org/geysermc/connector/utils/ResourcePack.java @@ -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 PACKS = new HashMap<>(); - public static final NativeCode 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 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; }