Repository navigation
Description
Description
Provide full implementation of RFC 4211 for UUID as part of the Guid type.
Having hash based predictible Guids (v5) which could be derived from string and don't collide with random based guids (v4) can be extremely useful in some scenarios. RFC4122 proposes a standard around UUIDs to address that.
Possible Use Cases
Here are some of the use cases I see:
- Slug projection to Guid primary key (eliminate the need to maintain an index for that matter, allows using the primary key which is usually the clustered index to get the data)
- Mixing potentially identical ids but namespaced in the same keyspace.
- Reducing a composite key to a single Guid.
- More broadly every scenario where a hash is a good fit (not from a security perspective) but where we need guids.
Proposed API
public partial struct Guid {
public static readonly Guid DnsNamespace // 6ba7b810-9dad-11d1-80b4-00c04fd430c8
public static readonly Guid UrlNamespace // 6ba7b811-9dad-11d1-80b4-00c04fd430c8
public static Guid NewGuid();
public static Guid NewUuidV1();
public static Guid NewUuidV4();
public static Guid NewUuidV5(string value, Guid namespace);
public static Guid NewUuidV5(byte[] value, Guid namespace);
}
Details
We would obviously keep the existing NewGuid() which in windows calls UuidCreate. It seems that it uses UUID v4 under the hood.
In addition we would add NewGuidV1, NewGuidV4 et NewGuidV5 methods. They would map to the adequate UUID generation method.
Related Issues
Open Questions
Currently the byte order from the constructor and ToByteArray is a bit unatural as it just reflect the underlying struct as we could expect a more natural byte order. It could be worth it to add methods overload to read and write guid using the natural order.
See also
- Current implementation:
part 1 and part 2 - Node JS implementation
- Unoptimized quick and dirty Guid V5 c# implementation
Updates
- December 12, 2017: Provide a first API proposal
- January 18, 2017: Add use cases
Activity
joperezr commentedon Nov 21, 2017
@sandorfr are you interested on writing up a formal api proposal so that we can take a look at it?
sandorfr commentedon Nov 21, 2017
sandorfr commentedon Dec 11, 2017
Just updated the description accordingly.
joperezr commentedon Dec 18, 2017
@sandorfr from your proposal
can be extremely useful in some scenarioscan you please add some sample scenarios where this would be useful to help on the review?sandorfr commentedon Jan 18, 2018
@joperezr I've added a section with possible use cases
shmuelie commentedon Sep 11, 2019
In a project I had a need for a V4 UUID (implementation of an internal communication protocal). In my research I found that while System.Guid is a V4 UUID there is no guarantee that this will always be true. I therefore built my own Uuid type. While I can't say it's fully optimized, it is full compliant with RFC 4122 and provides logic for creating versions 2, 3, 4, and 5. I've copied the API below. (The implementation uses unsafe code in some places to prevent creating arrays.)
om-ha commentedon Feb 17, 2020
For reference, found a good implementation here
om-ha commentedon Feb 17, 2020
Please note version 4 requires specific bitwise manipulation. Also to be cryptographically secure it must not be generated with
Guid.NewGuid();sandorfr commentedon Feb 18, 2020
Nice. The real problem here is to get the proposal/API approved to make this a thing :)
om-ha commentedon Feb 18, 2020
@sandorfr In fact I ended up using GuidOne. Which accounts for Endianness unlike other libraries. It completed my Stackoverflow answer here.
shmuelie commentedon Feb 18, 2020
I'm happy to provide my implementation if wanted.
GrabYourPitchforks commentedon Jul 30, 2020
Thanks all for the discussion. Some random thoughts on this:
I don't think we should add any surface area to the
Guidtype. If we add factory methods, it's going to add confusion. "What's the difference between all of these, and which one should I use?" is going to come up.For the same reason, I wouldn't add a
Uuidtype. The range of validGuidandUuidvalues would be identical, so IMO there's no reason to have two exchange types. It would again lead to confusion.Creation of a v5 UUID involves using the SHA-1 hash algorithm. Introducing new code paths that leverage SHA-1 is generally forbidden across Microsoft codebases unless it's needed for some core compatibility or interop scenario. The demand for version 5 UUIDs is not high enough to justify an exception here.
Given all of these I'm inclined to say that we should close this issue. If there's evidence that there's significant community need for this functionality and that it's too onerous for devs to roll the implementation themselves, then we should reconsider. But so far I don't think either threshold is met.
PSanetra commentedon Jul 30, 2020
I think that is just a reason to document those new methods.
I agree
I guess the reason behind avoiding SHA-1 is that it is partially broken for cryptographic use cases. In case of creating UUID v5 this seems to be irrelevant as the hash is truncated to 128 bit anyway for creating the UUID.
shmuelie commentedon Jul 30, 2020
Personally, I agree
I disagree. This is a square and rectangle case. All
Guids are validUuids but not allUuids are validGuids.v3 UUID is created using MD5, another cracked algorithm. I would argue that since in both cases it is not being used for cryptography but ID usage.
How often this is needed I can't say, but implementing correctly can be a pain, even more so if you must do so in a performant way.
sandorfr commentedon Jul 31, 2020
I strongly disagree with this one. That's a terrible reason not to offer an API. Yes there will be people who can't read the docs, it's not for them that you should design things.
SHA-1 vulnerability in the context of cryptography really is irrelevant here.
Given how long it's been opened and the low activity and reactions on this, I agree with this one.
Also there's some decent implementations in some packages mentioned above.
GrabYourPitchforks commentedon Jul 31, 2020
@SamuelEnglard @sandorfr I understand the arguments here, but "it's not used for cryptographic purposes" is not a valid argument for granting an exception. "Broken" algorithms like SHA1 are forbidden for all purposes across Microsoft code bases unless it's needed for legacy compat reasons or to support some core scenario for the product. (That last category is generally for allowing things like networking protocols.)
Unless we can demonstrate clearly that usage of MD5 or SHA1 in UUID generation is a critical scenario that we absolutely must support, we would not get signoff from release management to ship this functionality.
shmuelie commentedon Aug 2, 2020
I can't say I agree but at the same time I'm not the person who implemented these rules.
Getting approval to include a dependency for exactly one type isn't always easy. If it comes as part of .NET though, no arguing needed.
KalleOlaviNiemitalo commentedon Aug 2, 2020
.NET Runtime already implements a SHA-1 GUID algorithm as part of EventSource, albeit with a hardcoded namespace. I guess that is justified by legacy compat.
runtime/src/libraries/System.Private.CoreLib/src/System/Diagnostics/Tracing/EventSource.cs
Lines 1525 to 1714 in 53976d3
GrabYourPitchforks commentedon Aug 2, 2020
Correct. You have no idea the sheer amount of paperwork we have to deal with every single release because of this and other compat-only usages. There's an increasing amount of bureaucracy every year. I think the goal is to make it so obnoxious that at a certain point we relent and say "fine, we're yanking it!" :)
mdonoughe commentedon Oct 14, 2020
Note: the EventSource implementation of this algorithm is not compliant with the RFC. It generates UUIDs that match ETW, which is what matters for EventSource, but it does not generate UUIDs that match standard implementations. The problems are related to endianness of the byte arrays and not setting the RFC variant flag.
GrabYourPitchforks commentedon Oct 14, 2020
Triaged this morning with the area owners. We don't believe this belongs in the BCL, and as mentioned in this thread there are good fully-featured packages in the ecosystem which provide this capability.