Skip to content

Commit

Permalink
Added JsonIO.getJsonHashString(json)
Browse files Browse the repository at this point in the history
  • Loading branch information
LatvianModder committed Oct 21, 2023
1 parent 25115e5 commit 49186e3
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 12 deletions.
15 changes: 14 additions & 1 deletion common/src/main/java/dev/latvian/mods/kubejs/util/JsonIO.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,14 @@
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.Writer;
import java.math.BigInteger;
import java.nio.file.Files;
import java.nio.file.Path;
import java.security.MessageDigest;
import java.util.Collection;
import java.util.HexFormat;
import java.util.Map;
import java.util.Objects;

public class JsonIO {
@HideFromJS
Expand Down Expand Up @@ -179,9 +183,18 @@ public static byte[] getJsonHashBytes(JsonElement json) {
} catch (IOException ex) {
ex.printStackTrace();
var h = json.hashCode();
return new byte[]{(byte) (h >> 24), (byte) (h >> 16), (byte) (h >> 8), (byte) (h >> 0)};
return new byte[]{(byte) (h >> 24), (byte) (h >> 16), (byte) (h >> 8), (byte) h};
}

return baos.toByteArray();
}

public static String getJsonHashString(JsonElement json) {
try {
var messageDigest = Objects.requireNonNull(MessageDigest.getInstance("MD5"));
return new BigInteger(HexFormat.of().formatHex(messageDigest.digest(JsonIO.getJsonHashBytes(json))), 16).toString(36);
} catch (Exception ex) {
return "%08x".formatted(json.hashCode());
}
}
}
12 changes: 1 addition & 11 deletions common/src/main/java/dev/latvian/mods/kubejs/util/UtilsJS.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,7 @@
import java.lang.reflect.Type;
import java.lang.reflect.TypeVariable;
import java.lang.reflect.WildcardType;
import java.math.BigInteger;
import java.nio.file.Path;
import java.security.MessageDigest;
import java.time.Duration;
import java.time.temporal.TemporalAmount;
import java.util.ArrayList;
Expand All @@ -85,10 +83,8 @@
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.HexFormat;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.Random;
import java.util.Set;
Expand Down Expand Up @@ -836,13 +832,7 @@ public static <T> String getUniqueId(T input, Codec<T> codec) {
}

private static <T> String getUniqueId(T input, Function<T, JsonElement> toJson) {
try {
var messageDigest = Objects.requireNonNull(MessageDigest.getInstance("MD5"));
var json = toJson.apply(input);
return new BigInteger(HexFormat.of().formatHex(messageDigest.digest(JsonIO.getJsonHashBytes(json))), 16).toString(36);
} catch (Exception ex) {
throw new RuntimeException("MD5 not supported", ex);
}
return JsonIO.getJsonHashString(toJson.apply(input));
}

public static String stripEventName(String s) {
Expand Down

0 comments on commit 49186e3

Please sign in to comment.