question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Would this library support generating namespaced UUIDs without a namespace?

See original GitHub issue

Is your feature request related to a problem? Please describe.

I am working with some other microservices implemented in Java, their acceptance tests use UUIDs which are generated from string names, and no namespaces, using this library: docs, source.

Describe the solution you’d like

I think it would be possible to include a similar option, which omits the namespace if it is not given.

Describe alternatives you’ve considered

I am considering just using the URL namespace, but i was hoping to raise as little eyebrows as possible for future workers who have to read my code. Tests are like documentation after all, and I was hoping to reflect the logic as closely as possible as the Java side.

It is also possible to give { length: 16 } as the argument instead of a uuid string, which achieves the desired result, but would be preferable to just give null as the argument, or something along those lines.

Additional context

Here is the logic which i was hoping to get from this library, in Java code (please forgive me, i couldnt get a job doing nodejs in my city):

    @Override
    public UUID generate(byte[] nameBytes)
    {
        byte[] digest;
        synchronized (_digester) {
            _digester.reset();
            if (_namespace != null) {
                _digester.update(UUIDUtil.asByteArray(_namespace));
            }
            _digester.update(nameBytes);
            digest = _digester.digest();
        }
        return UUIDUtil.constructUUID(_type, digest);
    }

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
Alan-Liangcommented, Feb 3, 2021

I came across this when trying to implement the Yggdrasil protocol for Minecraft; if you really need to generate the same UUID as UUID.nameUUIDFromBytes(bytes) in Java does, here is a hack to trick the library into doing this:

const uuid = require('uuid')
// copied from https://github.com/uuidjs/uuid/blob/7feb2c3e854964999afa9aad026058b400ed1388/src/v35.js#L4-L14
function stringToBytes (str) {
  str = unescape(encodeURIComponent(str)) // UTF8 escape
  const bytes = []
  for (let i = 0; i < str.length; ++i) bytes.push(str.charCodeAt(i))
  return bytes
}

const str = 'hello'
const bytes = stringToBytes(str)
const name = {
  ...bytes,
  get length () {
    return name.ok
      ? bytes.length
      : (name.ok = true, bytes.length - 16)
  },
}
const namespace = {
  get length () {
    return namespace.ok
      ? 0
      : (namespace.ok = true, 16)
  },
}
uuid.v5(name, namespace) // => 'aaf4c61d-dcc5-58a2-9abe-de0f3b482cd9'

It’s a hack anyway (so pin the npm package version), and I really hope we can achieve this in an easier way.

0reactions
ctavancommented, Nov 18, 2020

@alexanderankin yes, this is totally expected. The two different UUIDs you are generating with the JAVA library are:

hash(name) != hash(NIL + name)

where NIL are just 16 0-bytes.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Generating v5 UUID. What is name and namespace?
You can generate a large number of distinct logical namespaces using type 3 or type 5 UUIDs. First, create a root UUID for...
Read more >
issue with uuidv5 and/or parse function - Invalid UUID #511
A GUID generated in dotnet is failing to parse. We get an error: Invalid UUID. We encountered the issue with v5, but it's...
Read more >
An RFC-compliant library for working with UUIDs - 47 Degrees
Namespaced UUIDs are generated from a UUID (namespace) and a hashed value (name). V3 uses MD5 and V5 uses SHA1 hash. val namespace...
Read more >
Uuid Library - 1.73.0
Name Generator​​ RFC 4122 specifies that a name-based uuid is derived from content in a namespace. A uuid with identical content in an...
Read more >
A Universally Unique IDentifier (UUID) URN Namespace
As a result, generation on demand can be completely automated, and used for a variety of purposes. The UUID generation algorithm described here...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found