| 1 |
Go UUID implementation |
| 2 |
======================== |
| 3 |
|
| 4 |
[](https://raw.githubusercontent.com/twinj/uuid/master/LICENSE) |
| 5 |
[](http://godoc.org/github.com/twinj/uuid) |
| 6 |
[](https://ci.appveyor.com/project/myesui/uuid) |
| 7 |
[](https://travis-ci.org/twinj/uuid) |
| 8 |
[](https://coveralls.io/github/twinj/uuid?branch=master) |
| 9 |
|
| 10 |
**This project is currently pre 1.0.0** |
| 11 |
|
| 12 |
This package provides RFC 4122 and DCE 1.1 compliant UUIDs. |
| 13 |
It will generate the following: |
| 14 |
|
| 15 |
* Version 1: based on a Timestamp and MAC address as Node id |
| 16 |
* Version 2: based on DCE Security - Experimental |
| 17 |
* Version 3: based on MD5 hash |
| 18 |
* Version 4: based on cryptographically secure random numbers |
| 19 |
* Version 5: based on SHA-1 hash |
| 20 |
|
| 21 |
Functions NewV1, NewV2, NewV3, NewV4, NewV5, New, NewHex and Parse() for |
| 22 |
generating version 1, 2, 3, 4 and 5 Uuid's |
| 23 |
|
| 24 |
# Requirements |
| 25 |
|
| 26 |
Any supported version of Go. |
| 27 |
|
| 28 |
# Design considerations |
| 29 |
|
| 30 |
* V1 UUIDs should be properly sequential. This can cause the Generator to work |
| 31 |
more slowly compared to other implementations. It can however be manually tuned |
| 32 |
to have performance that is on par. This is achieved by setting the Timestamp |
| 33 |
Resolution. Benchmark tests have been provided to help determine the best |
| 34 |
setting for your server |
| 35 |
|
| 36 |
Proper test coverage has determined thant the UUID timestamp spinner works |
| 37 |
correctly, across multiple clock resolutions. The generator produces |
| 38 |
timestamps that roll out sequentially and will only modify the clock |
| 39 |
sequence on very rare circumstances. |
| 40 |
|
| 41 |
It is highly recommended that you register a uuid.Saver if you use V1 or V2 |
| 42 |
UUIDs as it will ensure a higher probability of uniqueness. |
| 43 |
|
| 44 |
Example V1 output: |
| 45 |
5fb1a280-30f0-11e6-9614-005056c00001 |
| 46 |
5fb1a281-30f0-11e6-9614-005056c00001 |
| 47 |
5fb1a282-30f0-11e6-9614-005056c00001 |
| 48 |
5fb1a283-30f0-11e6-9614-005056c00001 |
| 49 |
5fb1a284-30f0-11e6-9614-005056c00001 |
| 50 |
5fb1a285-30f0-11e6-9614-005056c00001 |
| 51 |
5fb1a286-30f0-11e6-9614-005056c00001 |
| 52 |
5fb1a287-30f0-11e6-9614-005056c00001 |
| 53 |
5fb1a288-30f0-11e6-9614-005056c00001 |
| 54 |
5fb1a289-30f0-11e6-9614-005056c00001 |
| 55 |
5fb1a28a-30f0-11e6-9614-005056c00001 |
| 56 |
5fb1a28b-30f0-11e6-9614-005056c00001 |
| 57 |
5fb1a28c-30f0-11e6-9614-005056c00001 |
| 58 |
5fb1a28d-30f0-11e6-9614-005056c00001 |
| 59 |
5fb1a28e-30f0-11e6-9614-005056c00001 |
| 60 |
5fb1a28f-30f0-11e6-9614-005056c00001 |
| 61 |
5fb1a290-30f0-11e6-9614-005056c00001 |
| 62 |
|
| 63 |
* The V1 UUID generator should work on all app servers |
| 64 |
To achieve this there are no Os locking threads or file system dependant |
| 65 |
storage. The uuid.Saver interface exists for the user to provide their own |
| 66 |
storage implementations. The package provides a uuid.Saver |
| 67 |
which works on a standard OS environment. |
| 68 |
* The V4 UUID should allow the user to choose how to handle any error that |
| 69 |
can occur in the CPRNG. The default is to panic. |
| 70 |
* The package should be able to handle multiple instances of Generator's so a |
| 71 |
* user can produce UUIDs from multiple sources. |
| 72 |
|
| 73 |
# Recent Changes |
| 74 |
|
| 75 |
* Added ability for user defined Generator's which can be setup with your own |
| 76 |
retrieval functions for a Node Id, Timestamp and Random data for a UUID; more |
| 77 |
details in docs. |
| 78 |
* Now builds in Windows, OsX and Linux, with test coverage checking and code |
| 79 |
quality checks. |
| 80 |
* Added V2 UUIDs |
| 81 |
* Improved builds and 100% test coverage |
| 82 |
* Library overhaul to cleanup exports that are not useful for a user |
| 83 |
* Improved and fixed file system uuid.Saver interface, breaking changes. |
| 84 |
To use a uuid.Saver make sure you pass it in via the |
| 85 |
uuid.RegisterSaver(Saver) function before a UUID is generated, so as to |
| 86 |
take affect. This is because only one attempt at system initialisation can |
| 87 |
be attempted. |
| 88 |
|
| 89 |
## Installation |
| 90 |
|
| 91 |
Use the `go` tool: |
| 92 |
|
| 93 |
$ go get github.com/twinj/uuid |
| 94 |
|
| 95 |
# Typical Usage |
| 96 |
|
| 97 |
See [documentation and examples](http://godoc.org/github.com/twinj/uuid) |
| 98 |
for more information. |
| 99 |
|
| 100 |
## All UUIDs |
| 101 |
|
| 102 |
import "github.com/twinj/uuid" |
| 103 |
|
| 104 |
id, _ := uuid.Parse("6ba7b810-9dad-11d1-80b4-00c04fd430c8") |
| 105 |
|
| 106 |
if uuid.Equal(id, uuid.NameSpaceDNS) { |
| 107 |
fmt.Println("Alas these are equal") |
| 108 |
} |
| 109 |
|
| 110 |
if uuid.Compare(id, uuid.NameSpaceDNS) == 0 { |
| 111 |
fmt.Println("They are also equal") |
| 112 |
} |
| 113 |
|
| 114 |
if uuid.Compare(id, uuid.NameSpaceX500) == -1 { |
| 115 |
fmt.Println("id < uuid.NameSpaceX500") |
| 116 |
} |
| 117 |
|
| 118 |
if uuid.Compare(uuid.NameSpaceX50, id) == 1 { |
| 119 |
fmt.Println("uuid.NameSpaceX500 > id") |
| 120 |
} |
| 121 |
|
| 122 |
// Default Format is FormatCanonical |
| 123 |
fmt.Println(uuid.Formatter(id, uuid.FormatCanonicalCurly)) |
| 124 |
|
| 125 |
uuid.SwitchFormat(uuid.FormatCanonicalBracket) |
| 126 |
|
| 127 |
|
| 128 |
## Version 1 and 2 UUIDs |
| 129 |
|
| 130 |
import "github.com/twinj/uuid" |
| 131 |
|
| 132 |
// An uuid.Init or uuid.Register* function must be called before any V1 or |
| 133 |
// V2 UUIDs, only needs 1 call. |
| 134 |
uuid.Init() |
| 135 |
|
| 136 |
id := uuid.NewV1() |
| 137 |
fmt.Println(id) |
| 138 |
fmt.Printf("version %s variant %x: %s\n", u1.Version(), u1.Variant(), id) |
| 139 |
|
| 140 |
id = uuid.NewV2(uuid.DomainUser) |
| 141 |
fmt.Println(id) |
| 142 |
fmt.Printf("version %s variant %x: %s\n", u1.Version(), u1.Variant(), id) |
| 143 |
|
| 144 |
// If you wish to register a saving mechanism to keep track of your UUID |
| 145 |
// It is recommeneded to add a Saver so as to reduce risk in UUID |
| 146 |
// collisions |
| 147 |
saver := new(savers.FileSystemSaver) |
| 148 |
saver.Report = true |
| 149 |
saver.Duration = time.Second * 3 |
| 150 |
|
| 151 |
// Must be called before any V1 or V2 UUIDs. Do not call uuid.Init if |
| 152 |
// registering a Saver |
| 153 |
uuid.RegisterSaver(saver) |
| 154 |
|
| 155 |
## Version 3 and 5 UUIDs |
| 156 |
|
| 157 |
import "github.com/twinj/uuid" |
| 158 |
|
| 159 |
// Does not need to be called first but is recommended |
| 160 |
uuid.Init() |
| 161 |
|
| 162 |
id := uuid.NewV3(uuid.NameSpaceURL, uuid.Name("www.example.com")) |
| 163 |
fmt.Println(id) |
| 164 |
fmt.Printf("version %s variant %x: %s\n", u1.Version(), u1.Variant(), id) |
| 165 |
|
| 166 |
id := uuid.NewV5(uuid.NameSpaceURL, uuid.Name("www.example.com")) |
| 167 |
fmt.Println(id) |
| 168 |
fmt.Printf("version %s variant %x: %s\n", u1.Version(), u1.Variant(), id) |
| 169 |
|
| 170 |
id = uuid.NewV5(uuid.NameSpaceURL, id) |
| 171 |
fmt.Println(id) |
| 172 |
fmt.Printf("version %s variant %x: %s\n", u1.Version(), u1.Variant(), id) |
| 173 |
|
| 174 |
## Version 4 UUIDs |
| 175 |
|
| 176 |
import "github.com/twinj/uuid" |
| 177 |
|
| 178 |
// Does not need to be called first but is recommended |
| 179 |
uuid.Init() |
| 180 |
|
| 181 |
// A V4 UUID will panic by default if the systems CPRNG fails - this can |
| 182 |
// be changed by registering your own generator |
| 183 |
u4 := uuid.NewV4() |
| 184 |
fmt.Println(id) |
| 185 |
fmt.Printf("version %d variant %x: %s\n", u4.Version(), u4.Variant(), u4) |
| 186 |
|
| 187 |
## Custom Generators |
| 188 |
|
| 189 |
import "github.com/twinj/uuid" |
| 190 |
|
| 191 |
// Improve resolution for V1 and 2 UUIDs |
| 192 |
// The resolution correlates to how many ids can be created before waiting |
| 193 |
// for the next unique timestamp. The default is a low 1024, this equates |
| 194 |
// to Ids that can be created in 100 nanoseconds. It is low to encourage |
| 195 |
// you to set it. |
| 196 |
uuid.RegisterGenerator(GeneratorConfig{Resolution: 18465}) |
| 197 |
|
| 198 |
// Provide your own node Id or MAC address |
| 199 |
uuid.RegisterGenerator(GeneratorConfig{ |
| 200 |
Id: func() uuid.Node{ |
| 201 |
// My Node Id |
| 202 |
// If this returns nil a random one will be generated |
| 203 |
}, |
| 204 |
}) |
| 205 |
|
| 206 |
// Replace the default Timestamp spinner with your own. |
| 207 |
uuid.RegisterGenerator(GeneratorConfig{ |
| 208 |
Next: func()(uuid.Timestamp){ |
| 209 |
// My own Timestamp function... |
| 210 |
// Resolution will become reduendant if you set this. |
| 211 |
// The package will increment the clock sequence if you produce equal Timestamps |
| 212 |
}, |
| 213 |
}) |
| 214 |
|
| 215 |
// Replace the default crypto/rand.Read CPRNG with your own. |
| 216 |
uuid.RegisterGenerator(GeneratorConfig{ |
| 217 |
Random: func([]byte)(int, error){ |
| 218 |
// My CPRNG function... |
| 219 |
}, |
| 220 |
}) |
| 221 |
|
| 222 |
// Replace the default error handler for V4 UUIDs. This function is called |
| 223 |
// when there is an error in the CPRNG. The default function causes a panic. |
| 224 |
// You can change that behaviour and handle the error by checking for nil |
| 225 |
// on a NewV4() call. |
| 226 |
// id := NewV4() |
| 227 |
// if id == nil { |
| 228 |
// err := uuid.Error() |
| 229 |
// // handle error |
| 230 |
// } |
| 231 |
// Trying again could fix the problem. Errors could be due to a lack of |
| 232 |
// system entropy or some other serious issue. These issues are rare, |
| 233 |
// however, having the tools to handle such issues is important. |
| 234 |
// This approach was taken as each user of this package will want to handle |
| 235 |
// this differently. |
| 236 |
uuid.RegisterGenerator(GeneratorConfig{ |
| 237 |
HandleError: func(error)bool{ |
| 238 |
// My HandleError function... |
| 239 |
// If this returns true the V4 generator will try again - if it |
| 240 |
// fails again the NewV4() function will exit with a nil |
| 241 |
// If this returns false the NewV4() function will exit with a nil |
| 242 |
}, |
| 243 |
}) |
| 244 |
|
| 245 |
// You can also just manage your own completely. |
| 246 |
gen := NewGenerator(GeneratorConfig{}) |
| 247 |
|
| 248 |
id := gen.NewV4() |
| 249 |
|
| 250 |
|
| 251 |
## Coverage |
| 252 |
|
| 253 |
* go test -coverprofile cover.out github.com/twinj/uuid |
| 254 |
* go tool cover -html=cover.out -o cover.html |
| 255 |
|
| 256 |
## Links |
| 257 |
|
| 258 |
* [RFC 4122](http://www.ietf.org/rfc/rfc4122.txt) |
| 259 |
* [DCE 1.1: Authentication and Security Services](http://pubs.opengroup.org/onlinepubs/9629399/apdxa.htm) |
| 260 |
|
| 261 |
## Copyright |
| 262 |
|
| 263 |
Copyright (C) 2016 [email protected] |
| 264 |
See [LICENSE](https://github.com/twinj/uuid/tree/master/LICENSE) |
| 265 |
file for details. |