179 lines
5 KiB
Dart
179 lines
5 KiB
Dart
import 'dart:convert';
|
|
import 'dart:math';
|
|
|
|
import 'package:crypto/crypto.dart';
|
|
import 'package:libac_flutter/nbt/Stream.dart';
|
|
|
|
class UUID {
|
|
late final int LSB;
|
|
late final int MSB;
|
|
late final List<int> _bytes;
|
|
|
|
UUID(int msb, int lsb) {
|
|
MSB = msb;
|
|
LSB = lsb;
|
|
|
|
ByteLayer layer = ByteLayer();
|
|
layer.writeLong(MSB);
|
|
layer.writeLong(LSB);
|
|
layer.resetPosition();
|
|
|
|
_bytes = layer.readBytes(16);
|
|
}
|
|
|
|
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 msb = 0;
|
|
int lsb = 0;
|
|
|
|
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();
|
|
msb = layer.readLong();
|
|
lsb = layer.readLong();
|
|
|
|
return UUID(msb, lsb);
|
|
} 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)}';
|
|
}
|
|
|
|
/// Returns the Most Significant Bits (MSB) long value of the UUID.
|
|
int getMostSignificantBits() => MSB;
|
|
|
|
/// Returns the Least Significant Bits (LSB) long value of the UUID.
|
|
int getLeastSignificantBits() => LSB;
|
|
|
|
/// 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) {
|
|
return UUID.generate(4);
|
|
}
|
|
} else
|
|
params = parameters!;
|
|
switch (version) {
|
|
case 0:
|
|
return UUID(0, 0);
|
|
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();
|
|
|
|
var msb = layer.readUnsignedLong();
|
|
var lsb = layer.readUnsignedLong();
|
|
|
|
layer.resetPosition();
|
|
return UUID(msb, lsb);
|
|
}
|
|
case 4:
|
|
{
|
|
ByteLayer layer = ByteLayer();
|
|
final random = Random.secure();
|
|
|
|
layer.writeLong(
|
|
(random.nextInt(0xFFFFFFFF) << 32) | random.nextInt(0xFFFFFFFF));
|
|
layer.writeLong(
|
|
(random.nextInt(0xFFFFFFFF) << 32) | random.nextInt(0xFFFFFFFF));
|
|
|
|
layer.unsetSetBit(6, 0xF0, 0x40);
|
|
layer.unsetSetBit(8, 0xC0, 0x80);
|
|
|
|
layer.resetPosition();
|
|
|
|
return UUID(layer.readUnsignedLong(), layer.readUnsignedLong());
|
|
}
|
|
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.isEmpty) {
|
|
final namespaceBytes = utf8.encode(namespace);
|
|
layer.writeBytes(namespaceBytes);
|
|
}
|
|
|
|
if (!name.isEmpty) {
|
|
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.readUnsignedLong(), layer.readUnsignedLong());
|
|
}
|
|
default:
|
|
throw ArgumentError('Unsupported UUID version: $version');
|
|
}
|
|
}
|
|
}
|