Skip to content

Instantly share code, notes, and snippets.

@RedyAu
Created August 17, 2021 14:33
Show Gist options
  • Save RedyAu/bba8774189e7841f09523f474f21bb61 to your computer and use it in GitHub Desktop.
Save RedyAu/bba8774189e7841f09523f474f21bb61 to your computer and use it in GitHub Desktop.
[DART] Generate an offline minecraft UUID v3 based on the case sensitive player name
/*
Written by Balint66 and RedyAu, based on https://gist.github.com/games647/2b6a00a8fc21fd3b88375f03c9e2e603
Make sure to import the necessary crypto package in pubspec.yaml:
dependencies:
crypto: ^3.0.1
*/
import 'dart:convert';
import 'dart:typed_data';
import 'package:crypto/crypto.dart';
void main() {
print(getUUID("Notch"));
}
String getUUID(String name) {
//Get a byte list from the hash of the username
Uint8List hash = Uint8List.fromList(
md5.convert(utf8.encode("OfflinePlayer:" + name)).bytes);
//Set bytes to indicate:
//Version 3: name based md5hash
hash[6] = hash[6] & 0x0f | 0x30;
//IETF variant
hash[8] = hash[8] & 0x3f | 0x80;
var uid = "";
var dashes = [4, 6, 8, 10];
//Get hex string from byte list, and include dashes to get final formatting
for (var i = 0; i < hash.length; i += 2) {
int sec = 0;
if (i + 1 < hash.length) {
sec = hash[i + 1];
}
if (dashes.contains(i)) {
uid += "-";
}
uid += ((hash[i] << 8) | sec).toRadixString(16);
}
return (uid);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment