UUID Name Based Version
Derive a version 3 UUID with nameUUIDFromBytes, check its version and variant, and contrast it with a random version 4 UUID.
Code
import java.nio.charset.StandardCharsets;
import java.util.UUID;
public class Main {
public static void main(String[] args) {
byte[] name = "devexamples.com/examples/uuid".getBytes(StandardCharsets.UTF_8);
// nameUUIDFromBytes derives a version 3 UUID from the bytes
UUID key = UUID.nameUUIDFromBytes(name);
System.out.println("uuid: " + key);
System.out.println("version: " + key.version());
System.out.println("variant: " + key.variant());
// the same bytes always produce the same identifier
UUID again = UUID.nameUUIDFromBytes(name);
System.out.println("stable: " + key.equals(again));
// different input bytes produce a different identifier
UUID other = UUID.nameUUIDFromBytes(
"devexamples.com/examples/other".getBytes(StandardCharsets.UTF_8));
System.out.println("different: " + !key.equals(other));
// randomUUID uses version 4 instead of a name hash
UUID random = UUID.randomUUID();
System.out.println("random version: " + random.version());
System.out.println("random differs: " + !random.equals(key));
}
}How the snippet works
The moving parts are the class Main and the function main.
What to watch for
- Nothing arrives from a registry; the language ships every API used.
Using it
The primary signature is public static void main(String[] args)
Output lines are prefixed (uuid:, version:, variant:), making each step’s effect easy to spot.
Key statements
byte[] name = "devexamples.com/examples/uuid".getBytes(StandardCharsets.UTF_8);
UUID again = UUID.nameUUIDFromBytes(name);
UUID key = UUID.nameUUIDFromBytes(name);
Interactive Tools for This Topic
SRI Hash GeneratorGenerate Subresource Integrity (SRI) hashes from resource content using SHA-256, SHA-384, or SHA-512, and copy a ready-to-use script tag.UUID Version DetectorValidate the 8-4-4-4-12 UUID layout and read its version (v1 to v8) and variant nibbles per RFC 9562, with full field decomposition.bcrypt Cost EstimatorEstimate bcrypt cost factor latency and brute-force resistance, and get a recommended cost for your target hash time and attacker throughput.Argon2id Hash InfoEstimate Argon2id password-hash latency and memory usage, and check your parameters against OWASP 2024 minimum and recommended settings.
FAQ
- What does UUID Name Based Version print when run?
- Invoke the file as-is — the `uuid:`, `version:`, `variant:` prefixes identify which step printed each line.
Report a problem with this exampleOpens a pre-filled issue on GitHub.