168 lines
4.4 KiB
Dart
168 lines
4.4 KiB
Dart
import 'dart:convert';
|
|
import 'dart:math';
|
|
|
|
import 'package:crypto/crypto.dart';
|
|
|
|
import '../../nbt/Stream.dart';
|
|
|
|
class UUID {
|
|
late final List<int> _bytes;
|
|
|
|
UUID(List<int> bytes) {
|
|
_bytes = bytes;
|
|
}
|
|
|
|
static final UUID ZERO = UUID.generate(0);
|
|
|
|
/// Validates whether the given [uuid] is a valid UUID.
|
|
static bool validate(String uuid) {
|
|
if (uuid.length == ((16 * 2) + 4)) {
|
|
return true; // Likely is true. This is just a surface level check
|
|
} else {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
/// Parses the given [uuid] string and returns a UUID object.
|
|
static UUID parse(String uuid) {
|
|
if (validate(uuid)) {
|
|
final segments = uuid.split('-');
|
|
if (segments.length != 5) {
|
|
throw const FormatException('Invalid UUID format');
|
|
}
|
|
|
|
final msbString = segments.sublist(0, 3).join('');
|
|
final lsbString = segments.sublist(3, 5).join('');
|
|
|
|
int i = 0;
|
|
ByteLayer layer = ByteLayer();
|
|
for (i = 0; i < msbString.length; i += 2) {
|
|
String hex = msbString.substring(i, i + 2);
|
|
int byte = int.parse(hex, radix: 16);
|
|
layer.writeByte(byte);
|
|
}
|
|
|
|
for (i = 0; i < lsbString.length; i += 2) {
|
|
String hex = lsbString.substring(i, i + 2);
|
|
int byte = int.parse(hex, radix: 16);
|
|
layer.writeByte(byte);
|
|
}
|
|
|
|
layer.resetPosition();
|
|
|
|
return UUID(layer.readBytes(16));
|
|
} else {
|
|
return UUID.ZERO;
|
|
}
|
|
}
|
|
|
|
@override
|
|
String toString() {
|
|
String hexBuilder = "";
|
|
for (int byte in _bytes) {
|
|
hexBuilder += byte.toRadixString(16).padLeft(2, '0');
|
|
}
|
|
return '${hexBuilder.substring(0, 8)}-${hexBuilder.substring(8, 12)}-${hexBuilder.substring(12, 16)}-${hexBuilder.substring(16, 20)}-${hexBuilder.substring(20, 32)}';
|
|
}
|
|
|
|
/// Factory method to generate UUID of the specific version.
|
|
factory UUID.generate(int version, {List<Object>? parameters}) {
|
|
List<Object> params = [];
|
|
if (parameters == null) {
|
|
if (version != 4 && version != 0) {
|
|
return UUID.generate(4);
|
|
}
|
|
} else {
|
|
params = parameters;
|
|
}
|
|
switch (version) {
|
|
case 0:
|
|
{
|
|
ByteLayer layer = ByteLayer();
|
|
layer.writeLong(0);
|
|
layer.writeLong(0);
|
|
|
|
layer.resetPosition();
|
|
|
|
return UUID(layer.readBytes(16));
|
|
}
|
|
case 3:
|
|
{
|
|
if (params.length != 2) {
|
|
throw Exception(
|
|
"UUID v3 requires two parameters, [namespace,name]");
|
|
}
|
|
String namespace = params[0] as String;
|
|
String name = params[1] as String;
|
|
|
|
ByteLayer layer = ByteLayer();
|
|
|
|
final namespaceBytes = utf8.encode(namespace);
|
|
layer.writeBytes(namespaceBytes);
|
|
final nameBytes = utf8.encode(name);
|
|
layer.writeBytes(nameBytes);
|
|
|
|
var bytes = md5.convert(List.from(layer.bytes)).bytes;
|
|
layer.clear();
|
|
layer.writeBytes(bytes);
|
|
|
|
layer.unsetSetBit(6, 0xF0, 0x30);
|
|
layer.unsetSetBit(8, 0xC0, 0x80);
|
|
|
|
layer.resetPosition();
|
|
return UUID(layer.readBytes(16));
|
|
}
|
|
case 4:
|
|
{
|
|
ByteLayer layer = ByteLayer();
|
|
final random = Random.secure();
|
|
|
|
layer.insertRandomBytes(16);
|
|
|
|
layer.unsetSetBit(6, 0xF0, 0x40);
|
|
layer.unsetSetBit(8, 0xC0, 0x80);
|
|
|
|
layer.resetPosition();
|
|
|
|
return UUID(layer.readBytes(16));
|
|
}
|
|
case 5:
|
|
{
|
|
ByteLayer layer = ByteLayer();
|
|
if (params.length != 2) {
|
|
throw Exception(
|
|
"UUID v5 requires two parameters, [namespace,name]");
|
|
}
|
|
String namespace = params[0] as String;
|
|
String name = params[1] as String;
|
|
|
|
if (namespace.isNotEmpty) {
|
|
final namespaceBytes = utf8.encode(namespace);
|
|
layer.writeBytes(namespaceBytes);
|
|
}
|
|
|
|
if (name.isNotEmpty) {
|
|
final nameBytes = utf8.encode(name);
|
|
layer.writeBytes(nameBytes);
|
|
}
|
|
|
|
final hashBytes = sha1.convert(List.from(layer.bytes)).bytes;
|
|
layer.clear();
|
|
layer.writeBytes(hashBytes);
|
|
|
|
layer.unsetSetBit(6, 0xF0, 0x50);
|
|
layer.unsetSetBit(8, 0xC0, 0x80);
|
|
|
|
layer.resetPosition();
|
|
|
|
return UUID(layer.readBytes(16));
|
|
}
|
|
default:
|
|
throw ArgumentError('Unsupported UUID version: $version');
|
|
}
|
|
}
|
|
|
|
Iterable<int> getBytes() {
|
|
return _bytes.take(16);
|
|
}
|
|
}
|