-
Notifications
You must be signed in to change notification settings - Fork 408
feat: Add support for UUID Version 8 (RFC 9562) #178
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ashwingopalsamy
wants to merge
5
commits into
google:master
Choose a base branch
from
ashwingopalsamy:feature/uuidv8
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
ee97a20
feature: add UUID version8 support
ashwingopalsamy a6b2edc
tests: add UUID version8 tests
ashwingopalsamy bbbda9c
fix: address code review comments
ashwingopalsamy f36b58e
fix: remove mu.Unlock
ashwingopalsamy 9d9f0cb
Update version8.go
ashwingopalsamy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,150 @@ | ||
| // Copyright 2024 Google Inc. All rights reserved. | ||
| // Use of this source code is governed by a BSD-style | ||
| // license that can be found in the LICENSE file. | ||
|
|
||
| package uuid | ||
|
|
||
| import ( | ||
| "encoding/binary" | ||
| "io" | ||
| "time" | ||
| ) | ||
|
|
||
| var ( | ||
| lastTimestamp uint64 | ||
| sequence uint16 | ||
| ) | ||
|
|
||
| // NewV8 generates a version 8 UUID. | ||
| // It sets the version and variant fields and fills the rest with random data. | ||
| // It allows embedding of user-defined data while maintaining the UUID structure. | ||
| // | ||
| // The layout includes: | ||
| // - custom_a: 48 bits (user-defined) | ||
| // - ver: 4 bits (set to 0b1000 for version 8) | ||
| // - custom_b: 12 bits (user-defined) | ||
| // - var: 2 bits (set to 0b10 for RFC 4122 variant) | ||
| // - custom_c: 62 bits (user-defined) | ||
| // | ||
| // For details, see https://datatracker.ietf.org/doc/html/rfc9562#name-uuid-version-8 | ||
| // | ||
| // NewV8 generates a UUID version 8 with completely random user-defined fields. | ||
| // It uses the randomness pool if enabled or falls back to a secure random source. | ||
| // On error, NewV8 returns Nil and an error. | ||
| func NewV8() (UUID, error) { | ||
| uuid, err := NewRandom() | ||
| if err != nil { | ||
| return uuid, err | ||
| } | ||
| uuid[6] = (uuid[6] & 0x0F) | 0x80 // Set version to 8 | ||
| uuid[8] = (uuid[8] & 0x3F) | 0x80 // Set variant to RFC 4122 | ||
| return uuid, nil | ||
| } | ||
|
|
||
| // NewV8FromReader generates a version 8 UUID with user-defined custom_a and custom_b. | ||
| // It uses random bits for custom_c if no random reader is provided. | ||
| // On error, NewV8FromReader returns Nil and an error. | ||
| func NewV8FromReader(customA, customB uint64, random io.Reader) (UUID, error) { | ||
| var uuid UUID | ||
|
|
||
| // Encode custom_a (48 bits) | ||
| binary.BigEndian.PutUint64(uuid[:8], customA) | ||
| copy(uuid[:6], uuid[2:8]) // Retain only the lower 48 bits | ||
|
|
||
| // Set version and custom_b (12 bits) | ||
| uuid[6] = (uuid[6] & 0x0F) | 0x80 | ||
| binary.BigEndian.PutUint16(uuid[6:8], uint16(customB)|0x8000) | ||
|
|
||
| // Fill custom_c (62 bits) | ||
| if random == nil { | ||
| random = rander | ||
| } | ||
| if _, err := io.ReadFull(random, uuid[8:]); err != nil { | ||
| return Nil, err | ||
| } | ||
| uuid[8] = (uuid[8] & 0x3F) | 0x80 | ||
|
|
||
| return uuid, nil | ||
| } | ||
|
|
||
| // NewV8TimeBased generates a version 8 UUID with a time-based custom_a field. | ||
| // It ensures uniqueness using a sequence number for UUIDs created in the same nanosecond. | ||
| func NewV8TimeBased(random io.Reader) (UUID, error) { | ||
| var uuid UUID | ||
| timestamp := uint64(time.Now().UnixNano()) | ||
|
|
||
| timeMu.Lock() | ||
| defer timeMu.Unlock() | ||
|
|
||
| if timestamp == lastTimestamp { | ||
| sequence++ | ||
| } else { | ||
| lastTimestamp = timestamp | ||
| sequence = 0 | ||
| } | ||
|
|
||
| // Encode timestamp into custom_a (48 bits) | ||
| binary.BigEndian.PutUint64(uuid[:8], timestamp) | ||
| copy(uuid[:6], uuid[2:8]) | ||
|
|
||
| // Set version and variant | ||
| uuid[6] = (uuid[6] & 0x0F) | 0x80 | ||
| uuid[8] = (uuid[8] & 0x3F) | 0x80 | ||
|
|
||
| // Add sequence to custom_c (16 bits) | ||
| binary.BigEndian.PutUint16(uuid[8:], sequence) | ||
|
|
||
| // Fill the rest with custom_c | ||
| if random == nil { | ||
| for i := 10; i < 16; i++ { | ||
| uuid[i] = 0 | ||
| } | ||
| } else if _, err := io.ReadFull(random, uuid[10:]); err != nil { | ||
| return Nil, err | ||
| } | ||
|
|
||
| return uuid, nil | ||
| } | ||
|
|
||
| // makeV8 generates a version 8 UUID using user-provided or random data for custom_a, custom_b, and custom_c. | ||
| func makeV8(uuid []byte, customA, customB, customC []byte) { | ||
|
||
| /* | ||
| Layout of UUIDv8: | ||
| 0 1 2 3 | ||
| 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 | ||
| +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | ||
| | custom_a | | ||
| +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | ||
| | custom_a | ver | custom_b | | ||
| +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | ||
| |var| custom_c | | ||
| +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | ||
| | custom_c | | ||
| +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | ||
| */ | ||
|
|
||
| _ = uuid[15] // Bounds check | ||
|
|
||
| // Fill custom_a (48 bits) | ||
| if customA != nil { | ||
| copy(uuid[0:6], customA) | ||
| } else { | ||
| randomBits(uuid[0:6]) | ||
| } | ||
|
|
||
| // Set version and fill custom_b (12 bits) | ||
| uuid[6] = (uuid[6] & 0x0F) | 0x80 | ||
| if customB != nil { | ||
| copy(uuid[6:8], customB) | ||
| } else { | ||
| randomBits(uuid[6:8]) | ||
| } | ||
| uuid[8] = (uuid[8] & 0x3F) | 0x80 | ||
|
|
||
| // Fill custom_c (62 bits) | ||
| if customC != nil { | ||
| copy(uuid[8:], customC) | ||
| } else { | ||
| randomBits(uuid[8:]) | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we release lock after the
ifbelow?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🤔 Sure, I can do that.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not like that. Drop
defer, it's not needed after theif.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Alrights! done.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
defer, notmu.Unlock()