Last active
March 28, 2024 16:10
-
-
Save unascribed/70e830d471d6a3272e3f to your computer and use it in GitHub Desktop.
How to generate a (correct) Minecraft-style hex digest. Tested.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This software is licensed under the CC0 Public Domain Dedication. | |
See https://creativecommons.org/publicdomain/zero/1.0/ for more details. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.unascribed.brokenhash; | |
import java.math.BigInteger; | |
import java.security.MessageDigest; | |
import java.security.NoSuchAlgorithmException; | |
import java.nio.charset.StandardCharsets; | |
/** | |
* Generates a broken Minecraft-style twos-complement signed | |
* hex digest. Tested and confirmed to match vanilla. | |
*/ | |
public class BrokenHash { | |
public static String hash(String str) { | |
try { | |
byte[] digest = digest(str, "SHA-1"); | |
return new BigInteger(digest).toString(16); | |
} catch (NoSuchAlgorithmException e) { | |
throw new RuntimeException(e); | |
} | |
} | |
private static byte[] digest(String str, String algorithm) throws NoSuchAlgorithmException { | |
MessageDigest md = MessageDigest.getInstance(algorithm); | |
byte[] strBytes = str.getBytes(StandardCharsets.UTF_8); | |
return md.digest(strBytes); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Gist apparently doesn't notify me when comments are posted. 😠
@mcjp78 - No, the server ID is always blank in recent versions of Minecraft.
@deprilula28 - Sure; note that I've upgraded my hardware since I wrote this originally:
Both methods are very fast and there is no reason to use MethodTwo. I wrote it to learn how twos-complement and such works.
Also note that I have learned since I wrote this that the method I use to benchmark is fundamentally broken. I should have used an existing microbenchmark library, but I didn't, and as a result the test sucks and isn't really meaningful.
EDIT: I've removed the second method and the benchmark from the Gist.