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.FileOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.function.Function; import java.util.function.Function;
public class FileUtils { public class FileUtils {
@ -59,7 +61,7 @@ public class FileUtils {
} }
public static <T> T loadJson(InputStream src, Class<T> valueType) throws IOException { 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); return objectMapper.readValue(src, valueType);
} }
@ -152,4 +154,21 @@ public class FileUtils {
} }
return stream; 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; 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 org.geysermc.connector.GeyserConnector;
import java.io.File; import java.io.File;
import java.nio.file.Files;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.zip.ZipFile; import java.util.zip.ZipFile;
public class ResourcePack { public class ResourcePack {
public static final Map<String, ResourcePack> PACKS = new HashMap<>(); 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; public static final int CHUNK_SIZE = 102400;
private byte[] sha256; private byte[] sha256;
@ -23,14 +17,6 @@ public class ResourcePack {
private ResourcePackManifest.Version version; private ResourcePackManifest.Version version;
public static void loadPacks() { 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"); File directory = new File("packs");
if (!directory.exists()) { if (!directory.exists()) {
@ -41,7 +27,7 @@ public class ResourcePack {
if(file.getName().endsWith(".zip") || file.getName().endsWith(".mcpack")) { if(file.getName().endsWith(".zip") || file.getName().endsWith(".mcpack")) {
ResourcePack pack = new ResourcePack(); ResourcePack pack = new ResourcePack();
pack.sha256 = getBytes(hashes.get(file.getName())); pack.sha256 = FileUtils.calculateSHA256(file);
try { try {
ZipFile zip = new ZipFile(file); 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() { public byte[] getSha256() {
return sha256; return sha256;
} }