From 5226866a7440c007848934c72baaaf487f5b3d95 Mon Sep 17 00:00:00 2001 From: Frank van der Heijden Date: Mon, 22 Feb 2021 15:56:39 +0100 Subject: [PATCH] Remove commons-codec as dep for MD hashing --- .../common/tasks/PluginWatcherTask.java | 2 +- .../serverutils/common/utils/FileUtils.java | 18 ++++++++++-------- .../serverutils/common/utils/StringUtils.java | 18 ++++++++++++++++++ build.gradle | 1 - 4 files changed, 29 insertions(+), 10 deletions(-) diff --git a/Common/src/main/java/net/frankheijden/serverutils/common/tasks/PluginWatcherTask.java b/Common/src/main/java/net/frankheijden/serverutils/common/tasks/PluginWatcherTask.java index 17a530c..2503b59 100644 --- a/Common/src/main/java/net/frankheijden/serverutils/common/tasks/PluginWatcherTask.java +++ b/Common/src/main/java/net/frankheijden/serverutils/common/tasks/PluginWatcherTask.java @@ -70,7 +70,7 @@ public class PluginWatcherTask extends AbstractTask { for (WatchEvent event : key.pollEvents()) { if (file.getName().equals(event.context().toString())) { String previousHash = hash; - hash = FileUtils.getHash(file); + hash = FileUtils.getHash(file.toPath()); if (task != null) { //noinspection unchecked taskManager.cancelTask(task); diff --git a/Common/src/main/java/net/frankheijden/serverutils/common/utils/FileUtils.java b/Common/src/main/java/net/frankheijden/serverutils/common/utils/FileUtils.java index 91bc17d..e9e63de 100644 --- a/Common/src/main/java/net/frankheijden/serverutils/common/utils/FileUtils.java +++ b/Common/src/main/java/net/frankheijden/serverutils/common/utils/FileUtils.java @@ -2,15 +2,16 @@ package net.frankheijden.serverutils.common.utils; import com.google.gson.JsonElement; import com.google.gson.JsonParser; -import org.apache.commons.codec.digest.DigestUtils; import java.io.BufferedReader; import java.io.File; -import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.nio.charset.StandardCharsets; import java.nio.file.Files; +import java.nio.file.Path; +import java.security.MessageDigest; +import java.security.NoSuchAlgorithmException; public class FileUtils { @@ -36,17 +37,18 @@ public class FileUtils { } /** - * Get the Hash of a file. + * Get the Hash of a file at given path. * - * @param file The file + * @param path The path * @return The file's hash */ - public static String getHash(File file) { + public static String getHash(Path path) { + byte[] digest; try { - return DigestUtils.md5Hex(new FileInputStream(file)); - } catch (IOException e) { - // Shouldn't happen + digest = MessageDigest.getInstance("MD5").digest(Files.readAllBytes(path)); + } catch (IOException | NoSuchAlgorithmException ex) { return null; } + return StringUtils.bytesToHex(digest); } } diff --git a/Common/src/main/java/net/frankheijden/serverutils/common/utils/StringUtils.java b/Common/src/main/java/net/frankheijden/serverutils/common/utils/StringUtils.java index 8f5be91..a0c2eeb 100644 --- a/Common/src/main/java/net/frankheijden/serverutils/common/utils/StringUtils.java +++ b/Common/src/main/java/net/frankheijden/serverutils/common/utils/StringUtils.java @@ -1,5 +1,7 @@ package net.frankheijden.serverutils.common.utils; +import java.nio.charset.StandardCharsets; + public class StringUtils { /** @@ -44,4 +46,20 @@ public class StringUtils { } return sb.substring(1); } + + private static final byte[] HEX_ARRAY = "0123456789abcdef".getBytes(StandardCharsets.US_ASCII); + + /** + * Converts a bytes array to hex. + * via https://stackoverflow.com/a/9855338/11239174 + */ + public static String bytesToHex(byte[] bytes) { + byte[] hexChars = new byte[bytes.length * 2]; + for (int j = 0; j < bytes.length; j++) { + int v = bytes[j] & 0xFF; + hexChars[j * 2] = HEX_ARRAY[v >>> 4]; + hexChars[j * 2 + 1] = HEX_ARRAY[v & 0x0F]; + } + return new String(hexChars, StandardCharsets.UTF_8); + } } diff --git a/build.gradle b/build.gradle index 79b1d55..f061886 100644 --- a/build.gradle +++ b/build.gradle @@ -24,7 +24,6 @@ subprojects { } dependencies { - implementation 'commons-codec:commons-codec:1.15' implementation 'com.github.FrankHeijden:MinecraftReflection:1.0.0' testCompile 'org.assertj:assertj-core:3.18.1'