Skip to content

Instantly share code, notes, and snippets.

@mjtb49
Last active February 24, 2022 02:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mjtb49/a0d0821c71e6d16138f91aab9377084e to your computer and use it in GitHub Desktop.
Save mjtb49/a0d0821c71e6d16138f91aab9377084e to your computer and use it in GitHub Desktop.
import java.util.Random;
import java.util.UUID;
public class UUIDBonker {
public static UUID randomUuid(Random random) {
//fabric mappings seem to call m and l the other way round. This disagrees with UUID constructor.
long m = random.nextLong() & -61441L | 16384L;
long l = random.nextLong() & 4611686018427387903L | Long.MIN_VALUE;
return new UUID(m,l);
}
public static long crackUUIDBits(long lsb, long msb) {
long min1 = lsb >>> 32;
//bitshift introduces potential rounding error. Also the constant in this line can maybe be pulled through most of the code
long min2 = (msb - (11718085204285L >>> 16)) & 0xffff_ffffL;
long max2 = min2 + 1;
// generated by (((-11718085204285L << 48) >> 48) * -41139260) >> 16, it fixes rounding error
long bugFixer = 14340612L;
long a1 = (-28703124L * min1 + -41139260L * min2 + bugFixer) >> 32;
long a2 = ( -4751153L * min1 + 2996749L * max2) >> 32;
return (( a1 * -2996749L + a2 * -41139260L ) * 246154705703781L + 107048004364969L) & 0xffff_ffff_ffffL;
}
public static long crackUUIDString(String uuid) {
uuid = uuid.replace("-","");
return crackUUIDBits(Long.parseUnsignedLong(uuid.substring(0,16), 16), Long.parseUnsignedLong(uuid.substring(16,32), 16));
}
public static void main(String[] args) {
int failcount = 0;
//example UUID from a 1.16.1 villager
String villagerUUID= "3355f18a-21c8-4f86-aeb6-f3191f681b6f";
System.out.println(villagerUUID.equals(randomUuid(new Random(crackUUIDString(villagerUUID) ^ 0x5deece66dL)).toString()));
for (int i = 0; i < 10000000; i++) {
long s = new Random().nextLong() & 0xffff_ffff_ffffL;
Random r = new Random(s ^ 0x5deece66dL);
if (crackUUIDString(randomUuid(r).toString()) != s) {
failcount += 1;
}
}
System.out.println(failcount);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment