f4b6a3 / uuid-creator

A UUID library for Java. Fully compliant with the new Internet standard published as RFC 9562. It was developed following the evolution of standard drafts.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

UUID Creator

This is a Java library for generating Universally Unique Identifiers.

The Internet standard RFC 9562 was published in May 2024, making RFC 4122 obsolete. This library is fully compliant with the new RFC, as it was developed following the evolution of the new standard until its publication.

List of implemented UUID subtypes:

  • UUID Version 1: the Gregorian time-based UUID specified in RFC 9562;
  • UUID Version 2: the DCE Security version, with embedded POSIX UIDs, specified in DCE 1.1;
  • UUID Version 3: the name-based version that uses MD5 hashing specified in RFC 9562;
  • UUID Version 4: The randomly or pseudorandomly generated version specified in RFC 9562;
  • UUID Version 5: the name-based version that uses SHA-1 hashing specified in RFC 9562;
  • UUID Version 6: the reordered Gregorian time-based UUID specified in RFC 9562;
  • UUID Version 7: the Unix Epoch time-based UUID specified in RFC 9562.

This library solves some of the JDK's UUID issues:

Problem Solution
UUID can't generate Gregorian time-based UUIDs (UUIDv1). Use:
UUID can't generate SHA-1 UUIDs (UUIDv5). Use:
UUID has no validation method, which makes developers use UUID.fromString() or regular expression for validation. Use:
UUID.nameUUIDFromBytes(), which generates MD5 UUIDs (UUIDv3), does not have a namespace parameter as required by the standard. Use:
Some methods such as UUID.timestamp() are strongly related to UUIDv1, even though it's impossible to generate UUIDv1. Use UuidUtil.
UUID.randomUUID() can be slow due to lack of entropy in the operating system. Use:
However, keep in mind that none of them are cryptographically secure.
UUID.compareTo() behaves unexpectedly due to signed long comparisons, causing non-alphabetical sorting. Use UuidComparator.
UUID.fromString() allows non-canonical strings like 0-0-0-0-0 as valid UUID strings. Use:

This project contains a micro benchmark and a good amount of unit tests.

Read the Wiki pages and the Javadocs.

NOTE: This software is not supported or maintained by any organization. This information may be useful if you believe that having an organization behind a project is a criterion for deciding whether software can be adopted or not.

Dependency

Maven:

<dependency>
  <groupId>com.github.f4b6a3</groupId>
  <artifactId>uuid-creator</artifactId>
  <version>5.3.7</version>
</dependency>

Gradle:

implementation 'com.github.f4b6a3:uuid-creator:5.3.3'

See more options in maven.org.

HINT: The jar file can be downloaded directly from maven.org.

Modularity

Module and bundle names are the same as the root package name.

  • JPMS module name: com.github.f4b6a3.uuid
  • OSGi symbolic name: com.github.f4b6a3.uuid

Usage

All UUID types can be created from the facade UuidCreator.

The goal of this class is to make most of the library's functionality available in a single place so that you developers don't have to worry about the internals of the library. All you need is to decide which type of UUID you need for your application and call the respective generation method. If in doubt, read the documentation and check the source code.

Create a UUIDv1:

UUID uuid = UuidCreator.getTimeBased();

Create a UUIDv2:

UUID uuid = UuidCreator.getDceSecurity(UuidLocalDomain.LOCAL_DOMAIN_PERSON, 1234);

Create a UUIDv3:

UUID uuid = UuidCreator.getNameBasedMd5(UuidNamespace.NAMESPACE_URL, "https://github.com/");

Create a UUIDv4:

UUID uuid = UuidCreator.getRandomBased();

Create a UUIDv5:

UUID uuid = UuidCreator.getNameBasedSha1(UuidNamespace.NAMESPACE_URL, "https://github.com/");

Create a UUIDv6:

UUID uuid = UuidCreator.getTimeOrdered();

Create a UUIDv7:

UUID uuid = UuidCreator.getTimeOrderedEpoch();

NOTE: A UUID version is a UUID subtype. The word "version" is not used in the sense that a higher version number makes the previous one obsolete. There are 8 subtypes of UUID and each of them is assigned a number; for example, a UUIDv7 is a UUID of subtype 7. Likewise, a UUID variant is a UUID type. There are 4 types of UUID: (1) the prehistoric one, (2) the one described in RFC 9562, (3) the one belonging to Microsoft and (4) the one reserved for the future. RFC 9562 retains the terms “version” and “variant” for compatibility with previous specifications and existing implementations.

PERSONAL NOTE: The library can do a lot more than the examples above (much more than I should have done). So I sincerely hope that most people are satisfied with this. In other words, your like is my payment. Anther thing I want to say is that the name of this software is UUID Creator or uuid-creator. Use "f4b6a3" or "com.github.f4b6a3" only when necessary to avoid doubt, as this is just a unique package name to follow Java convention (I know it wasn't a good idea, but I can't change it anymore).

Alternative API

GUID is an alternative implementation to the classic JDK's UUID. It also serves as a standalone generator, independent from the rest of the library. This may result in fewer classes being loaded.

This new API was also designed to be an alternative to UuidCreator with three goals in mind: clean interface, simple implementation, and high performance. It was inspired by popular libraries for Javascript and Python.

Additionaly, it does not block during GUID generation due to the non-cryptographic random number generator used by its factory methods. However, it is not recommended when the security of “cryptographic quality” generators is considered necessary.

GUID guid = GUID.v1();
GUID guid = GUID.v2(GUID.LOCAL_DOMAIN_PERSON, 1234);
GUID guid = GUID.v3(GUID.NAMESPACE_DNS, "www.example.com");
GUID guid = GUID.v4();
GUID guid = GUID.v5(GUID.NAMESPACE_DNS, "www.example.com");
GUID guid = GUID.v6();
GUID guid = GUID.v7();

Generate JDK's UUID from GUID's API

You can generate JDK's UUIDs using GUID's API. For example, you can generate a JDK's UUID version 7 with this simple statement:

UUID uuid = GUID.v7().toUUID();

When you call toUUID() the internal value of GUID is copied to the new JDK's UUID.

Deprecation

The methods which use a UUID as a "name" parameter such as UuidCreator.getNameBasedMd5(UUID name) are deprecated. They will be removed soon after the new RFC is published. For more details, please read #91.

The v8() method of the alternative GUID class is also deprecated and will be removed soon.

Other identifier generators

Check out the other ID generators from the same family:

License

This library is Open Source software released under the MIT license.

About

A UUID library for Java. Fully compliant with the new Internet standard published as RFC 9562. It was developed following the evolution of standard drafts.

License:MIT License


Languages

Language:Java 99.9%Language:Shell 0.1%Language:Batchfile 0.0%