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
Update to RFC 9562 #117
Merged
Merged
Update to RFC 9562 #117
Changes from 10 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
b2fb5f3
update draft links in documentation
kohenkatz 93764ef
update README to newest draft version; fix minimum supported based on…
kohenkatz a273888
code and comment updates for draft 14
kohenkatz a4c9c2d
code and comment changes for draft 14
kohenkatz 10d645a
fix comment format
kohenkatz aef03d5
note which draft changed v6 clock_seq to full random
kohenkatz 5f2ba72
move helper function after public functions
kohenkatz beea71e
wip: update pointing to released RFC
kohenkatz 9fa75ca
finish updating rfc references to new number
kohenkatz 81691df
remove remaining draft references
kohenkatz e2280ed
Update generator.go
kohenkatz File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains 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 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 |
---|---|---|
|
@@ -70,24 +70,12 @@ func NewV5(ns UUID, name string) UUID { | |
// NewV6 returns a k-sortable UUID based on a timestamp and 48 bits of | ||
// pseudorandom data. The timestamp in a V6 UUID is the same as V1, with the bit | ||
// order being adjusted to allow the UUID to be k-sortable. | ||
// | ||
// This is implemented based on revision 03 of the Peabody UUID draft, and may | ||
// be subject to change pending further revisions. Until the final specification | ||
// revision is finished, changes required to implement updates to the spec will | ||
// not be considered a breaking change. They will happen as a minor version | ||
// releases until the spec is final. | ||
func NewV6() (UUID, error) { | ||
return DefaultGenerator.NewV6() | ||
} | ||
|
||
// NewV7 returns a k-sortable UUID based on the current millisecond precision | ||
// UNIX epoch and 74 bits of pseudorandom data. It supports single-node batch generation (multiple UUIDs in the same timestamp) with a Monotonic Random counter. | ||
// | ||
// This is implemented based on revision 04 of the Peabody UUID draft, and may | ||
// be subject to change pending further revisions. Until the final specification | ||
// revision is finished, changes required to implement updates to the spec will | ||
// not be considered a breaking change. They will happen as a minor version | ||
// releases until the spec is final. | ||
func NewV7() (UUID, error) { | ||
return DefaultGenerator.NewV7() | ||
} | ||
|
@@ -103,7 +91,7 @@ type Generator interface { | |
} | ||
|
||
// Gen is a reference UUID generator based on the specifications laid out in | ||
// RFC-4122 and DCE 1.1: Authentication and Security Services. This type | ||
// RFC-9562 and DCE 1.1: Authentication and Security Services. This type | ||
// satisfies the Generator interface as defined in this package. | ||
// | ||
// For consumers who are generating V1 UUIDs, but don't want to expose the MAC | ||
|
@@ -242,7 +230,7 @@ func (g *Gen) NewV1() (UUID, error) { | |
copy(u[10:], hardwareAddr) | ||
|
||
u.SetVersion(V1) | ||
u.SetVariant(VariantRFC4122) | ||
u.SetVariant(VariantRFC9562) | ||
|
||
return u, nil | ||
} | ||
|
@@ -251,7 +239,7 @@ func (g *Gen) NewV1() (UUID, error) { | |
func (g *Gen) NewV3(ns UUID, name string) UUID { | ||
u := newFromHash(md5.New(), ns, name) | ||
u.SetVersion(V3) | ||
u.SetVariant(VariantRFC4122) | ||
u.SetVariant(VariantRFC9562) | ||
|
||
return u | ||
} | ||
|
@@ -263,7 +251,7 @@ func (g *Gen) NewV4() (UUID, error) { | |
return Nil, err | ||
} | ||
u.SetVersion(V4) | ||
u.SetVariant(VariantRFC4122) | ||
u.SetVariant(VariantRFC9562) | ||
|
||
return u, nil | ||
} | ||
|
@@ -272,92 +260,59 @@ func (g *Gen) NewV4() (UUID, error) { | |
func (g *Gen) NewV5(ns UUID, name string) UUID { | ||
u := newFromHash(sha1.New(), ns, name) | ||
u.SetVersion(V5) | ||
u.SetVariant(VariantRFC4122) | ||
u.SetVariant(VariantRFC9562) | ||
|
||
return u | ||
} | ||
|
||
// NewV6 returns a k-sortable UUID based on a timestamp and 48 bits of | ||
// pseudorandom data. The timestamp in a V6 UUID is the same as V1, with the bit | ||
// order being adjusted to allow the UUID to be k-sortable. | ||
// | ||
// This is implemented based on revision 03 of the Peabody UUID draft, and may | ||
// be subject to change pending further revisions. Until the final specification | ||
// revision is finished, changes required to implement updates to the spec will | ||
// not be considered a breaking change. They will happen as a minor version | ||
// releases until the spec is final. | ||
func (g *Gen) NewV6() (UUID, error) { | ||
/* https://datatracker.ietf.org/doc/html/rfc9562#name-uuid-version-6 | ||
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 | ||
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | ||
| time_high | | ||
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | ||
| time_mid | ver | time_low | | ||
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | ||
|var| clock_seq | node | | ||
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | ||
| node | | ||
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ */ | ||
var u UUID | ||
|
||
if _, err := io.ReadFull(g.rand, u[10:]); err != nil { | ||
return Nil, err | ||
} | ||
|
||
timeNow, clockSeq, err := g.getClockSequence(false) | ||
timeNow, _, err := g.getClockSequence(false) | ||
if err != nil { | ||
return Nil, err | ||
} | ||
|
||
binary.BigEndian.PutUint32(u[0:], uint32(timeNow>>28)) // set time_high | ||
binary.BigEndian.PutUint16(u[4:], uint16(timeNow>>12)) // set time_mid | ||
binary.BigEndian.PutUint16(u[6:], uint16(timeNow&0xfff)) // set time_low (minus four version bits) | ||
binary.BigEndian.PutUint16(u[8:], clockSeq&0x3fff) // set clk_seq_hi_res (minus two variant bits) | ||
|
||
u.SetVersion(V6) | ||
u.SetVariant(VariantRFC4122) | ||
|
||
return u, nil | ||
} | ||
|
||
// getClockSequence returns the epoch and clock sequence for V1,V6 and V7 UUIDs. | ||
// | ||
// When useUnixTSMs is false, it uses the Coordinated Universal Time (UTC) as a count of 100- | ||
// | ||
// nanosecond intervals since 00:00:00.00, 15 October 1582 (the date of Gregorian reform to the Christian calendar). | ||
func (g *Gen) getClockSequence(useUnixTSMs bool) (uint64, uint16, error) { | ||
var err error | ||
g.clockSequenceOnce.Do(func() { | ||
buf := make([]byte, 2) | ||
if _, err = io.ReadFull(g.rand, buf); err != nil { | ||
return | ||
} | ||
g.clockSequence = binary.BigEndian.Uint16(buf) | ||
}) | ||
if err != nil { | ||
return 0, 0, err | ||
//Based on the RFC 9562 recommendation that this data be fully random and not a monotonic counter, | ||
kohenkatz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
//we do NOT support batching version 6 UUIDs. | ||
//set clock_seq (14 bits) and node (48 bits) pseudo-random bits (first 2 bits will be overridden) | ||
if _, err = io.ReadFull(g.rand, u[8:]); err != nil { | ||
return Nil, err | ||
} | ||
|
||
g.storageMutex.Lock() | ||
defer g.storageMutex.Unlock() | ||
u.SetVersion(V6) | ||
|
||
var timeNow uint64 | ||
if useUnixTSMs { | ||
timeNow = uint64(g.epochFunc().UnixMilli()) | ||
} else { | ||
timeNow = g.getEpoch() | ||
} | ||
// Clock didn't change since last UUID generation. | ||
// Should increase clock sequence. | ||
if timeNow <= g.lastTime { | ||
g.clockSequence++ | ||
} | ||
g.lastTime = timeNow | ||
//overwrite first 2 bits of byte[8] for the variant | ||
u.SetVariant(VariantRFC9562) | ||
|
||
return timeNow, g.clockSequence, nil | ||
return u, nil | ||
} | ||
|
||
// NewV7 returns a k-sortable UUID based on the current millisecond precision | ||
// UNIX epoch and 74 bits of pseudorandom data. | ||
// | ||
// This is implemented based on revision 04 of the Peabody UUID draft, and may | ||
// be subject to change pending further revisions. Until the final specification | ||
// revision is finished, changes required to implement updates to the spec will | ||
// not be considered a breaking change. They will happen as a minor version | ||
// releases until the spec is final. | ||
func (g *Gen) NewV7() (UUID, error) { | ||
var u UUID | ||
/* https://www.ietf.org/archive/id/draft-peabody-dispatch-new-uuid-format-04.html#name-uuid-version-7 | ||
0 1 2 3 | ||
/* https://datatracker.ietf.org/doc/html/rfc9562#name-uuid-version-7 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same nit here |
||
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 | ||
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | ||
| unix_ts_ms | | ||
|
@@ -381,9 +336,11 @@ func (g *Gen) NewV7() (UUID, error) { | |
u[4] = byte(ms >> 8) | ||
u[5] = byte(ms) | ||
|
||
//support batching by using a monotonic pseudo-random sequence | ||
//Support batching by using a monotonic pseudo-random sequence, | ||
//as described in RFC 9562 section 6.2, Method 1. | ||
//The 6th byte contains the version and partially rand_a data. | ||
//We will lose the most significant bites from the clockSeq (with SetVersion), but it is ok, we need the least significant that contains the counter to ensure the monotonic property | ||
//We will lose the most significant bites from the clockSeq (with SetVersion), but it is ok, | ||
//we need the least significant that contains the counter to ensure the monotonic property | ||
binary.BigEndian.PutUint16(u[6:8], clockSeq) // set rand_a with clock seq which is random and monotonic | ||
|
||
//override first 4bits of u[6]. | ||
|
@@ -394,11 +351,48 @@ func (g *Gen) NewV7() (UUID, error) { | |
return Nil, err | ||
} | ||
//override first 2 bits of byte[8] for the variant | ||
u.SetVariant(VariantRFC4122) | ||
u.SetVariant(VariantRFC9562) | ||
|
||
return u, nil | ||
} | ||
|
||
// getClockSequence returns the epoch and clock sequence for V1,V6 and V7 UUIDs. | ||
// | ||
// When useUnixTSMs is false, it uses the Coordinated Universal Time (UTC) as a count of 100- | ||
// | ||
// nanosecond intervals since 00:00:00.00, 15 October 1582 (the date of Gregorian reform to the Christian calendar). | ||
func (g *Gen) getClockSequence(useUnixTSMs bool) (uint64, uint16, error) { | ||
var err error | ||
g.clockSequenceOnce.Do(func() { | ||
buf := make([]byte, 2) | ||
if _, err = io.ReadFull(g.rand, buf); err != nil { | ||
return | ||
} | ||
g.clockSequence = binary.BigEndian.Uint16(buf) | ||
}) | ||
if err != nil { | ||
return 0, 0, err | ||
} | ||
|
||
g.storageMutex.Lock() | ||
defer g.storageMutex.Unlock() | ||
|
||
var timeNow uint64 | ||
if useUnixTSMs { | ||
timeNow = uint64(g.epochFunc().UnixMilli()) | ||
} else { | ||
timeNow = g.getEpoch() | ||
} | ||
// Clock didn't change since last UUID generation. | ||
// Should increase clock sequence. | ||
if timeNow <= g.lastTime { | ||
g.clockSequence++ | ||
} | ||
g.lastTime = timeNow | ||
|
||
return timeNow, g.clockSequence, nil | ||
} | ||
|
||
// Returns the hardware address. | ||
func (g *Gen) getHardwareAddr() ([]byte, error) { | ||
var err error | ||
|
@@ -414,7 +408,7 @@ func (g *Gen) getHardwareAddr() ([]byte, error) { | |
if _, err = io.ReadFull(g.rand, g.hardwareAddr[:]); err != nil { | ||
return | ||
} | ||
// Set multicast bit as recommended by RFC-4122 | ||
// Set multicast bit as recommended by RFC-9562 | ||
g.hardwareAddr[0] |= 0x01 | ||
}) | ||
if err != nil { | ||
|
This file contains 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
Oops, something went wrong.
Oops, something went wrong.
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.
Nit: maybe include the source link in the godoc comment so that someone doesn't have to dive into the code to find it.
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.
@dylan-bourque I thought about that, but I think it's too much information that most users won't care about. The link is only there to show the origin of the bit layout below it, which is not something that needs to be in the godoc. The README has links to the full RFC, and that shows up in the godoc already.