pborman/uuid and google/uuid used to be different versions of
the same package, but now pborman/uuid is a compatibility wrapper
around google/uuid, maintained by the same person.
Clean up some of the usage as the functions differ slightly.
Not yet removed some uses of pborman/uuid in vendored code but
I have PRs in process for these.
Signed-off-by: Justin Cormack <justin.cormack@docker.com>
| ... | ... |
@@ -8,7 +8,7 @@ import ( |
| 8 | 8 |
|
| 9 | 9 |
"github.com/docker/docker/api/types" |
| 10 | 10 |
containertypes "github.com/docker/docker/api/types/container" |
| 11 |
- "github.com/pborman/uuid" |
|
| 11 |
+ "github.com/google/uuid" |
|
| 12 | 12 |
"gotest.tools/assert" |
| 13 | 13 |
is "gotest.tools/assert/cmp" |
| 14 | 14 |
) |
| ... | ... |
@@ -28,7 +28,7 @@ func TestMain(m *testing.M) {
|
| 28 | 28 |
|
| 29 | 29 |
func newContainer(t *testing.T) *Container {
|
| 30 | 30 |
var ( |
| 31 |
- id = uuid.New() |
|
| 31 |
+ id = uuid.New().String() |
|
| 32 | 32 |
cRoot = filepath.Join(root, id) |
| 33 | 33 |
) |
| 34 | 34 |
if err := os.MkdirAll(cRoot, 0755); err != nil {
|
| ... | ... |
@@ -11,8 +11,8 @@ import ( |
| 11 | 11 |
"github.com/docker/docker/api/types/filters" |
| 12 | 12 |
"github.com/docker/docker/container" |
| 13 | 13 |
"github.com/docker/docker/image" |
| 14 |
+ "github.com/google/uuid" |
|
| 14 | 15 |
"github.com/opencontainers/go-digest" |
| 15 |
- "github.com/pborman/uuid" |
|
| 16 | 16 |
"gotest.tools/assert" |
| 17 | 17 |
is "gotest.tools/assert/cmp" |
| 18 | 18 |
) |
| ... | ... |
@@ -36,7 +36,7 @@ func TestMain(m *testing.M) {
|
| 36 | 36 |
func setupContainerWithName(t *testing.T, name string, daemon *Daemon) *container.Container {
|
| 37 | 37 |
t.Helper() |
| 38 | 38 |
var ( |
| 39 |
- id = uuid.New() |
|
| 39 |
+ id = uuid.New().String() |
|
| 40 | 40 |
computedImageID = digest.FromString(id) |
| 41 | 41 |
cRoot = filepath.Join(root, id) |
| 42 | 42 |
) |
| ... | ... |
@@ -7,7 +7,7 @@ import ( |
| 7 | 7 |
"path/filepath" |
| 8 | 8 |
|
| 9 | 9 |
"github.com/docker/docker/pkg/ioutils" |
| 10 |
- "github.com/pborman/uuid" |
|
| 10 |
+ "github.com/google/uuid" |
|
| 11 | 11 |
) |
| 12 | 12 |
|
| 13 | 13 |
func loadOrCreateUUID(path string) (string, error) {
|
| ... | ... |
@@ -15,14 +15,21 @@ func loadOrCreateUUID(path string) (string, error) {
|
| 15 | 15 |
if err != nil {
|
| 16 | 16 |
return "", err |
| 17 | 17 |
} |
| 18 |
- id, err := ioutil.ReadFile(path) |
|
| 18 |
+ var id string |
|
| 19 |
+ idb, err := ioutil.ReadFile(path) |
|
| 19 | 20 |
if os.IsNotExist(err) {
|
| 20 |
- id = []byte(uuid.New()) |
|
| 21 |
- if err := ioutils.AtomicWriteFile(path, id, os.FileMode(0600)); err != nil {
|
|
| 21 |
+ id = uuid.New().String() |
|
| 22 |
+ if err := ioutils.AtomicWriteFile(path, []byte(id), os.FileMode(0600)); err != nil {
|
|
| 22 | 23 |
return "", fmt.Errorf("Error saving uuid file: %s", err)
|
| 23 | 24 |
} |
| 24 | 25 |
} else if err != nil {
|
| 25 | 26 |
return "", fmt.Errorf("Error loading uuid file %s: %s", path, err)
|
| 27 |
+ } else {
|
|
| 28 |
+ idp, err := uuid.Parse(string(idb)) |
|
| 29 |
+ if err != nil {
|
|
| 30 |
+ return "", fmt.Errorf("Error parsing uuid in file %s: %s", path, err)
|
|
| 31 |
+ } |
|
| 32 |
+ id = idp.String() |
|
| 26 | 33 |
} |
| 27 |
- return string(id), nil |
|
| 34 |
+ return id, nil |
|
| 28 | 35 |
} |
| ... | ... |
@@ -4,7 +4,7 @@ import ( |
| 4 | 4 |
"context" |
| 5 | 5 |
"testing" |
| 6 | 6 |
|
| 7 |
- "github.com/pborman/uuid" |
|
| 7 |
+ "github.com/google/uuid" |
|
| 8 | 8 |
"gotest.tools/assert" |
| 9 | 9 |
) |
| 10 | 10 |
|
| ... | ... |
@@ -15,6 +15,6 @@ func TestUUIDGeneration(t *testing.T) {
|
| 15 | 15 |
info, err := c.Info(context.Background()) |
| 16 | 16 |
assert.NilError(t, err) |
| 17 | 17 |
|
| 18 |
- id := uuid.Parse(info.ID) |
|
| 19 |
- assert.Equal(t, id != nil, true) |
|
| 18 |
+ _, err = uuid.Parse(info.ID) |
|
| 19 |
+ assert.NilError(t, err) |
|
| 20 | 20 |
} |
| ... | ... |
@@ -5,6 +5,7 @@ github.com/Microsoft/go-winio v0.4.11 |
| 5 | 5 |
github.com/docker/libtrust 9cbd2a1374f46905c68a4eb3694a130610adc62a |
| 6 | 6 |
github.com/go-check/check 4ed411733c5785b40214c70bce814c3a3a689609 https://github.com/cpuguy83/check.git |
| 7 | 7 |
github.com/golang/gddo 9b12a26f3fbd7397dee4e20939ddca719d840d2a |
| 8 |
+github.com/google/uuid v1.1.1 |
|
| 8 | 9 |
github.com/gorilla/mux v1.7.0 |
| 9 | 10 |
github.com/Microsoft/opengcs v0.3.9 |
| 10 | 11 |
github.com/kr/pty 5cf931ef8f |
| 11 | 12 |
new file mode 100644 |
| ... | ... |
@@ -0,0 +1,27 @@ |
| 0 |
+Copyright (c) 2009,2014 Google Inc. All rights reserved. |
|
| 1 |
+ |
|
| 2 |
+Redistribution and use in source and binary forms, with or without |
|
| 3 |
+modification, are permitted provided that the following conditions are |
|
| 4 |
+met: |
|
| 5 |
+ |
|
| 6 |
+ * Redistributions of source code must retain the above copyright |
|
| 7 |
+notice, this list of conditions and the following disclaimer. |
|
| 8 |
+ * Redistributions in binary form must reproduce the above |
|
| 9 |
+copyright notice, this list of conditions and the following disclaimer |
|
| 10 |
+in the documentation and/or other materials provided with the |
|
| 11 |
+distribution. |
|
| 12 |
+ * Neither the name of Google Inc. nor the names of its |
|
| 13 |
+contributors may be used to endorse or promote products derived from |
|
| 14 |
+this software without specific prior written permission. |
|
| 15 |
+ |
|
| 16 |
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
|
| 17 |
+"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
|
| 18 |
+LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
|
| 19 |
+A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
|
| 20 |
+OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
|
| 21 |
+SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
|
| 22 |
+LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
|
| 23 |
+DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
|
| 24 |
+THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
|
| 25 |
+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
|
| 26 |
+OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 0 | 27 |
new file mode 100644 |
| ... | ... |
@@ -0,0 +1,19 @@ |
| 0 |
+# uuid  |
|
| 1 |
+The uuid package generates and inspects UUIDs based on |
|
| 2 |
+[RFC 4122](http://tools.ietf.org/html/rfc4122) |
|
| 3 |
+and DCE 1.1: Authentication and Security Services. |
|
| 4 |
+ |
|
| 5 |
+This package is based on the github.com/pborman/uuid package (previously named |
|
| 6 |
+code.google.com/p/go-uuid). It differs from these earlier packages in that |
|
| 7 |
+a UUID is a 16 byte array rather than a byte slice. One loss due to this |
|
| 8 |
+change is the ability to represent an invalid UUID (vs a NIL UUID). |
|
| 9 |
+ |
|
| 10 |
+###### Install |
|
| 11 |
+`go get github.com/google/uuid` |
|
| 12 |
+ |
|
| 13 |
+###### Documentation |
|
| 14 |
+[](http://godoc.org/github.com/google/uuid) |
|
| 15 |
+ |
|
| 16 |
+Full `go doc` style documentation for the package can be viewed online without |
|
| 17 |
+installing this package by using the GoDoc site here: |
|
| 18 |
+http://godoc.org/github.com/google/uuid |
| 0 | 19 |
new file mode 100644 |
| ... | ... |
@@ -0,0 +1,80 @@ |
| 0 |
+// Copyright 2016 Google Inc. All rights reserved. |
|
| 1 |
+// Use of this source code is governed by a BSD-style |
|
| 2 |
+// license that can be found in the LICENSE file. |
|
| 3 |
+ |
|
| 4 |
+package uuid |
|
| 5 |
+ |
|
| 6 |
+import ( |
|
| 7 |
+ "encoding/binary" |
|
| 8 |
+ "fmt" |
|
| 9 |
+ "os" |
|
| 10 |
+) |
|
| 11 |
+ |
|
| 12 |
+// A Domain represents a Version 2 domain |
|
| 13 |
+type Domain byte |
|
| 14 |
+ |
|
| 15 |
+// Domain constants for DCE Security (Version 2) UUIDs. |
|
| 16 |
+const ( |
|
| 17 |
+ Person = Domain(0) |
|
| 18 |
+ Group = Domain(1) |
|
| 19 |
+ Org = Domain(2) |
|
| 20 |
+) |
|
| 21 |
+ |
|
| 22 |
+// NewDCESecurity returns a DCE Security (Version 2) UUID. |
|
| 23 |
+// |
|
| 24 |
+// The domain should be one of Person, Group or Org. |
|
| 25 |
+// On a POSIX system the id should be the users UID for the Person |
|
| 26 |
+// domain and the users GID for the Group. The meaning of id for |
|
| 27 |
+// the domain Org or on non-POSIX systems is site defined. |
|
| 28 |
+// |
|
| 29 |
+// For a given domain/id pair the same token may be returned for up to |
|
| 30 |
+// 7 minutes and 10 seconds. |
|
| 31 |
+func NewDCESecurity(domain Domain, id uint32) (UUID, error) {
|
|
| 32 |
+ uuid, err := NewUUID() |
|
| 33 |
+ if err == nil {
|
|
| 34 |
+ uuid[6] = (uuid[6] & 0x0f) | 0x20 // Version 2 |
|
| 35 |
+ uuid[9] = byte(domain) |
|
| 36 |
+ binary.BigEndian.PutUint32(uuid[0:], id) |
|
| 37 |
+ } |
|
| 38 |
+ return uuid, err |
|
| 39 |
+} |
|
| 40 |
+ |
|
| 41 |
+// NewDCEPerson returns a DCE Security (Version 2) UUID in the person |
|
| 42 |
+// domain with the id returned by os.Getuid. |
|
| 43 |
+// |
|
| 44 |
+// NewDCESecurity(Person, uint32(os.Getuid())) |
|
| 45 |
+func NewDCEPerson() (UUID, error) {
|
|
| 46 |
+ return NewDCESecurity(Person, uint32(os.Getuid())) |
|
| 47 |
+} |
|
| 48 |
+ |
|
| 49 |
+// NewDCEGroup returns a DCE Security (Version 2) UUID in the group |
|
| 50 |
+// domain with the id returned by os.Getgid. |
|
| 51 |
+// |
|
| 52 |
+// NewDCESecurity(Group, uint32(os.Getgid())) |
|
| 53 |
+func NewDCEGroup() (UUID, error) {
|
|
| 54 |
+ return NewDCESecurity(Group, uint32(os.Getgid())) |
|
| 55 |
+} |
|
| 56 |
+ |
|
| 57 |
+// Domain returns the domain for a Version 2 UUID. Domains are only defined |
|
| 58 |
+// for Version 2 UUIDs. |
|
| 59 |
+func (uuid UUID) Domain() Domain {
|
|
| 60 |
+ return Domain(uuid[9]) |
|
| 61 |
+} |
|
| 62 |
+ |
|
| 63 |
+// ID returns the id for a Version 2 UUID. IDs are only defined for Version 2 |
|
| 64 |
+// UUIDs. |
|
| 65 |
+func (uuid UUID) ID() uint32 {
|
|
| 66 |
+ return binary.BigEndian.Uint32(uuid[0:4]) |
|
| 67 |
+} |
|
| 68 |
+ |
|
| 69 |
+func (d Domain) String() string {
|
|
| 70 |
+ switch d {
|
|
| 71 |
+ case Person: |
|
| 72 |
+ return "Person" |
|
| 73 |
+ case Group: |
|
| 74 |
+ return "Group" |
|
| 75 |
+ case Org: |
|
| 76 |
+ return "Org" |
|
| 77 |
+ } |
|
| 78 |
+ return fmt.Sprintf("Domain%d", int(d))
|
|
| 79 |
+} |
| 0 | 80 |
new file mode 100644 |
| ... | ... |
@@ -0,0 +1,12 @@ |
| 0 |
+// Copyright 2016 Google Inc. All rights reserved. |
|
| 1 |
+// Use of this source code is governed by a BSD-style |
|
| 2 |
+// license that can be found in the LICENSE file. |
|
| 3 |
+ |
|
| 4 |
+// Package uuid generates and inspects UUIDs. |
|
| 5 |
+// |
|
| 6 |
+// UUIDs are based on RFC 4122 and DCE 1.1: Authentication and Security |
|
| 7 |
+// Services. |
|
| 8 |
+// |
|
| 9 |
+// A UUID is a 16 byte (128 bit) array. UUIDs may be used as keys to |
|
| 10 |
+// maps or compared directly. |
|
| 11 |
+package uuid |
| 0 | 12 |
new file mode 100644 |
| ... | ... |
@@ -0,0 +1,53 @@ |
| 0 |
+// Copyright 2016 Google Inc. All rights reserved. |
|
| 1 |
+// Use of this source code is governed by a BSD-style |
|
| 2 |
+// license that can be found in the LICENSE file. |
|
| 3 |
+ |
|
| 4 |
+package uuid |
|
| 5 |
+ |
|
| 6 |
+import ( |
|
| 7 |
+ "crypto/md5" |
|
| 8 |
+ "crypto/sha1" |
|
| 9 |
+ "hash" |
|
| 10 |
+) |
|
| 11 |
+ |
|
| 12 |
+// Well known namespace IDs and UUIDs |
|
| 13 |
+var ( |
|
| 14 |
+ NameSpaceDNS = Must(Parse("6ba7b810-9dad-11d1-80b4-00c04fd430c8"))
|
|
| 15 |
+ NameSpaceURL = Must(Parse("6ba7b811-9dad-11d1-80b4-00c04fd430c8"))
|
|
| 16 |
+ NameSpaceOID = Must(Parse("6ba7b812-9dad-11d1-80b4-00c04fd430c8"))
|
|
| 17 |
+ NameSpaceX500 = Must(Parse("6ba7b814-9dad-11d1-80b4-00c04fd430c8"))
|
|
| 18 |
+ Nil UUID // empty UUID, all zeros |
|
| 19 |
+) |
|
| 20 |
+ |
|
| 21 |
+// NewHash returns a new UUID derived from the hash of space concatenated with |
|
| 22 |
+// data generated by h. The hash should be at least 16 byte in length. The |
|
| 23 |
+// first 16 bytes of the hash are used to form the UUID. The version of the |
|
| 24 |
+// UUID will be the lower 4 bits of version. NewHash is used to implement |
|
| 25 |
+// NewMD5 and NewSHA1. |
|
| 26 |
+func NewHash(h hash.Hash, space UUID, data []byte, version int) UUID {
|
|
| 27 |
+ h.Reset() |
|
| 28 |
+ h.Write(space[:]) |
|
| 29 |
+ h.Write(data) |
|
| 30 |
+ s := h.Sum(nil) |
|
| 31 |
+ var uuid UUID |
|
| 32 |
+ copy(uuid[:], s) |
|
| 33 |
+ uuid[6] = (uuid[6] & 0x0f) | uint8((version&0xf)<<4) |
|
| 34 |
+ uuid[8] = (uuid[8] & 0x3f) | 0x80 // RFC 4122 variant |
|
| 35 |
+ return uuid |
|
| 36 |
+} |
|
| 37 |
+ |
|
| 38 |
+// NewMD5 returns a new MD5 (Version 3) UUID based on the |
|
| 39 |
+// supplied name space and data. It is the same as calling: |
|
| 40 |
+// |
|
| 41 |
+// NewHash(md5.New(), space, data, 3) |
|
| 42 |
+func NewMD5(space UUID, data []byte) UUID {
|
|
| 43 |
+ return NewHash(md5.New(), space, data, 3) |
|
| 44 |
+} |
|
| 45 |
+ |
|
| 46 |
+// NewSHA1 returns a new SHA1 (Version 5) UUID based on the |
|
| 47 |
+// supplied name space and data. It is the same as calling: |
|
| 48 |
+// |
|
| 49 |
+// NewHash(sha1.New(), space, data, 5) |
|
| 50 |
+func NewSHA1(space UUID, data []byte) UUID {
|
|
| 51 |
+ return NewHash(sha1.New(), space, data, 5) |
|
| 52 |
+} |
| 0 | 53 |
new file mode 100644 |
| ... | ... |
@@ -0,0 +1,37 @@ |
| 0 |
+// Copyright 2016 Google Inc. All rights reserved. |
|
| 1 |
+// Use of this source code is governed by a BSD-style |
|
| 2 |
+// license that can be found in the LICENSE file. |
|
| 3 |
+ |
|
| 4 |
+package uuid |
|
| 5 |
+ |
|
| 6 |
+import "fmt" |
|
| 7 |
+ |
|
| 8 |
+// MarshalText implements encoding.TextMarshaler. |
|
| 9 |
+func (uuid UUID) MarshalText() ([]byte, error) {
|
|
| 10 |
+ var js [36]byte |
|
| 11 |
+ encodeHex(js[:], uuid) |
|
| 12 |
+ return js[:], nil |
|
| 13 |
+} |
|
| 14 |
+ |
|
| 15 |
+// UnmarshalText implements encoding.TextUnmarshaler. |
|
| 16 |
+func (uuid *UUID) UnmarshalText(data []byte) error {
|
|
| 17 |
+ id, err := ParseBytes(data) |
|
| 18 |
+ if err == nil {
|
|
| 19 |
+ *uuid = id |
|
| 20 |
+ } |
|
| 21 |
+ return err |
|
| 22 |
+} |
|
| 23 |
+ |
|
| 24 |
+// MarshalBinary implements encoding.BinaryMarshaler. |
|
| 25 |
+func (uuid UUID) MarshalBinary() ([]byte, error) {
|
|
| 26 |
+ return uuid[:], nil |
|
| 27 |
+} |
|
| 28 |
+ |
|
| 29 |
+// UnmarshalBinary implements encoding.BinaryUnmarshaler. |
|
| 30 |
+func (uuid *UUID) UnmarshalBinary(data []byte) error {
|
|
| 31 |
+ if len(data) != 16 {
|
|
| 32 |
+ return fmt.Errorf("invalid UUID (got %d bytes)", len(data))
|
|
| 33 |
+ } |
|
| 34 |
+ copy(uuid[:], data) |
|
| 35 |
+ return nil |
|
| 36 |
+} |
| 0 | 37 |
new file mode 100644 |
| ... | ... |
@@ -0,0 +1,90 @@ |
| 0 |
+// Copyright 2016 Google Inc. All rights reserved. |
|
| 1 |
+// Use of this source code is governed by a BSD-style |
|
| 2 |
+// license that can be found in the LICENSE file. |
|
| 3 |
+ |
|
| 4 |
+package uuid |
|
| 5 |
+ |
|
| 6 |
+import ( |
|
| 7 |
+ "sync" |
|
| 8 |
+) |
|
| 9 |
+ |
|
| 10 |
+var ( |
|
| 11 |
+ nodeMu sync.Mutex |
|
| 12 |
+ ifname string // name of interface being used |
|
| 13 |
+ nodeID [6]byte // hardware for version 1 UUIDs |
|
| 14 |
+ zeroID [6]byte // nodeID with only 0's |
|
| 15 |
+) |
|
| 16 |
+ |
|
| 17 |
+// NodeInterface returns the name of the interface from which the NodeID was |
|
| 18 |
+// derived. The interface "user" is returned if the NodeID was set by |
|
| 19 |
+// SetNodeID. |
|
| 20 |
+func NodeInterface() string {
|
|
| 21 |
+ defer nodeMu.Unlock() |
|
| 22 |
+ nodeMu.Lock() |
|
| 23 |
+ return ifname |
|
| 24 |
+} |
|
| 25 |
+ |
|
| 26 |
+// SetNodeInterface selects the hardware address to be used for Version 1 UUIDs. |
|
| 27 |
+// If name is "" then the first usable interface found will be used or a random |
|
| 28 |
+// Node ID will be generated. If a named interface cannot be found then false |
|
| 29 |
+// is returned. |
|
| 30 |
+// |
|
| 31 |
+// SetNodeInterface never fails when name is "". |
|
| 32 |
+func SetNodeInterface(name string) bool {
|
|
| 33 |
+ defer nodeMu.Unlock() |
|
| 34 |
+ nodeMu.Lock() |
|
| 35 |
+ return setNodeInterface(name) |
|
| 36 |
+} |
|
| 37 |
+ |
|
| 38 |
+func setNodeInterface(name string) bool {
|
|
| 39 |
+ iname, addr := getHardwareInterface(name) // null implementation for js |
|
| 40 |
+ if iname != "" && addr != nil {
|
|
| 41 |
+ ifname = iname |
|
| 42 |
+ copy(nodeID[:], addr) |
|
| 43 |
+ return true |
|
| 44 |
+ } |
|
| 45 |
+ |
|
| 46 |
+ // We found no interfaces with a valid hardware address. If name |
|
| 47 |
+ // does not specify a specific interface generate a random Node ID |
|
| 48 |
+ // (section 4.1.6) |
|
| 49 |
+ if name == "" {
|
|
| 50 |
+ ifname = "random" |
|
| 51 |
+ randomBits(nodeID[:]) |
|
| 52 |
+ return true |
|
| 53 |
+ } |
|
| 54 |
+ return false |
|
| 55 |
+} |
|
| 56 |
+ |
|
| 57 |
+// NodeID returns a slice of a copy of the current Node ID, setting the Node ID |
|
| 58 |
+// if not already set. |
|
| 59 |
+func NodeID() []byte {
|
|
| 60 |
+ defer nodeMu.Unlock() |
|
| 61 |
+ nodeMu.Lock() |
|
| 62 |
+ if nodeID == zeroID {
|
|
| 63 |
+ setNodeInterface("")
|
|
| 64 |
+ } |
|
| 65 |
+ nid := nodeID |
|
| 66 |
+ return nid[:] |
|
| 67 |
+} |
|
| 68 |
+ |
|
| 69 |
+// SetNodeID sets the Node ID to be used for Version 1 UUIDs. The first 6 bytes |
|
| 70 |
+// of id are used. If id is less than 6 bytes then false is returned and the |
|
| 71 |
+// Node ID is not set. |
|
| 72 |
+func SetNodeID(id []byte) bool {
|
|
| 73 |
+ if len(id) < 6 {
|
|
| 74 |
+ return false |
|
| 75 |
+ } |
|
| 76 |
+ defer nodeMu.Unlock() |
|
| 77 |
+ nodeMu.Lock() |
|
| 78 |
+ copy(nodeID[:], id) |
|
| 79 |
+ ifname = "user" |
|
| 80 |
+ return true |
|
| 81 |
+} |
|
| 82 |
+ |
|
| 83 |
+// NodeID returns the 6 byte node id encoded in uuid. It returns nil if uuid is |
|
| 84 |
+// not valid. The NodeID is only well defined for version 1 and 2 UUIDs. |
|
| 85 |
+func (uuid UUID) NodeID() []byte {
|
|
| 86 |
+ var node [6]byte |
|
| 87 |
+ copy(node[:], uuid[10:]) |
|
| 88 |
+ return node[:] |
|
| 89 |
+} |
| 0 | 90 |
new file mode 100644 |
| ... | ... |
@@ -0,0 +1,12 @@ |
| 0 |
+// Copyright 2017 Google Inc. All rights reserved. |
|
| 1 |
+// Use of this source code is governed by a BSD-style |
|
| 2 |
+// license that can be found in the LICENSE file. |
|
| 3 |
+ |
|
| 4 |
+// +build js |
|
| 5 |
+ |
|
| 6 |
+package uuid |
|
| 7 |
+ |
|
| 8 |
+// getHardwareInterface returns nil values for the JS version of the code. |
|
| 9 |
+// This remvoves the "net" dependency, because it is not used in the browser. |
|
| 10 |
+// Using the "net" library inflates the size of the transpiled JS code by 673k bytes. |
|
| 11 |
+func getHardwareInterface(name string) (string, []byte) { return "", nil }
|
| 0 | 12 |
new file mode 100644 |
| ... | ... |
@@ -0,0 +1,33 @@ |
| 0 |
+// Copyright 2017 Google Inc. All rights reserved. |
|
| 1 |
+// Use of this source code is governed by a BSD-style |
|
| 2 |
+// license that can be found in the LICENSE file. |
|
| 3 |
+ |
|
| 4 |
+// +build !js |
|
| 5 |
+ |
|
| 6 |
+package uuid |
|
| 7 |
+ |
|
| 8 |
+import "net" |
|
| 9 |
+ |
|
| 10 |
+var interfaces []net.Interface // cached list of interfaces |
|
| 11 |
+ |
|
| 12 |
+// getHardwareInterface returns the name and hardware address of interface name. |
|
| 13 |
+// If name is "" then the name and hardware address of one of the system's |
|
| 14 |
+// interfaces is returned. If no interfaces are found (name does not exist or |
|
| 15 |
+// there are no interfaces) then "", nil is returned. |
|
| 16 |
+// |
|
| 17 |
+// Only addresses of at least 6 bytes are returned. |
|
| 18 |
+func getHardwareInterface(name string) (string, []byte) {
|
|
| 19 |
+ if interfaces == nil {
|
|
| 20 |
+ var err error |
|
| 21 |
+ interfaces, err = net.Interfaces() |
|
| 22 |
+ if err != nil {
|
|
| 23 |
+ return "", nil |
|
| 24 |
+ } |
|
| 25 |
+ } |
|
| 26 |
+ for _, ifs := range interfaces {
|
|
| 27 |
+ if len(ifs.HardwareAddr) >= 6 && (name == "" || name == ifs.Name) {
|
|
| 28 |
+ return ifs.Name, ifs.HardwareAddr |
|
| 29 |
+ } |
|
| 30 |
+ } |
|
| 31 |
+ return "", nil |
|
| 32 |
+} |
| 0 | 33 |
new file mode 100644 |
| ... | ... |
@@ -0,0 +1,59 @@ |
| 0 |
+// Copyright 2016 Google Inc. All rights reserved. |
|
| 1 |
+// Use of this source code is governed by a BSD-style |
|
| 2 |
+// license that can be found in the LICENSE file. |
|
| 3 |
+ |
|
| 4 |
+package uuid |
|
| 5 |
+ |
|
| 6 |
+import ( |
|
| 7 |
+ "database/sql/driver" |
|
| 8 |
+ "fmt" |
|
| 9 |
+) |
|
| 10 |
+ |
|
| 11 |
+// Scan implements sql.Scanner so UUIDs can be read from databases transparently |
|
| 12 |
+// Currently, database types that map to string and []byte are supported. Please |
|
| 13 |
+// consult database-specific driver documentation for matching types. |
|
| 14 |
+func (uuid *UUID) Scan(src interface{}) error {
|
|
| 15 |
+ switch src := src.(type) {
|
|
| 16 |
+ case nil: |
|
| 17 |
+ return nil |
|
| 18 |
+ |
|
| 19 |
+ case string: |
|
| 20 |
+ // if an empty UUID comes from a table, we return a null UUID |
|
| 21 |
+ if src == "" {
|
|
| 22 |
+ return nil |
|
| 23 |
+ } |
|
| 24 |
+ |
|
| 25 |
+ // see Parse for required string format |
|
| 26 |
+ u, err := Parse(src) |
|
| 27 |
+ if err != nil {
|
|
| 28 |
+ return fmt.Errorf("Scan: %v", err)
|
|
| 29 |
+ } |
|
| 30 |
+ |
|
| 31 |
+ *uuid = u |
|
| 32 |
+ |
|
| 33 |
+ case []byte: |
|
| 34 |
+ // if an empty UUID comes from a table, we return a null UUID |
|
| 35 |
+ if len(src) == 0 {
|
|
| 36 |
+ return nil |
|
| 37 |
+ } |
|
| 38 |
+ |
|
| 39 |
+ // assumes a simple slice of bytes if 16 bytes |
|
| 40 |
+ // otherwise attempts to parse |
|
| 41 |
+ if len(src) != 16 {
|
|
| 42 |
+ return uuid.Scan(string(src)) |
|
| 43 |
+ } |
|
| 44 |
+ copy((*uuid)[:], src) |
|
| 45 |
+ |
|
| 46 |
+ default: |
|
| 47 |
+ return fmt.Errorf("Scan: unable to scan type %T into UUID", src)
|
|
| 48 |
+ } |
|
| 49 |
+ |
|
| 50 |
+ return nil |
|
| 51 |
+} |
|
| 52 |
+ |
|
| 53 |
+// Value implements sql.Valuer so that UUIDs can be written to databases |
|
| 54 |
+// transparently. Currently, UUIDs map to strings. Please consult |
|
| 55 |
+// database-specific driver documentation for matching types. |
|
| 56 |
+func (uuid UUID) Value() (driver.Value, error) {
|
|
| 57 |
+ return uuid.String(), nil |
|
| 58 |
+} |
| 0 | 59 |
new file mode 100644 |
| ... | ... |
@@ -0,0 +1,123 @@ |
| 0 |
+// Copyright 2016 Google Inc. All rights reserved. |
|
| 1 |
+// Use of this source code is governed by a BSD-style |
|
| 2 |
+// license that can be found in the LICENSE file. |
|
| 3 |
+ |
|
| 4 |
+package uuid |
|
| 5 |
+ |
|
| 6 |
+import ( |
|
| 7 |
+ "encoding/binary" |
|
| 8 |
+ "sync" |
|
| 9 |
+ "time" |
|
| 10 |
+) |
|
| 11 |
+ |
|
| 12 |
+// A Time represents a time as the number of 100's of nanoseconds since 15 Oct |
|
| 13 |
+// 1582. |
|
| 14 |
+type Time int64 |
|
| 15 |
+ |
|
| 16 |
+const ( |
|
| 17 |
+ lillian = 2299160 // Julian day of 15 Oct 1582 |
|
| 18 |
+ unix = 2440587 // Julian day of 1 Jan 1970 |
|
| 19 |
+ epoch = unix - lillian // Days between epochs |
|
| 20 |
+ g1582 = epoch * 86400 // seconds between epochs |
|
| 21 |
+ g1582ns100 = g1582 * 10000000 // 100s of a nanoseconds between epochs |
|
| 22 |
+) |
|
| 23 |
+ |
|
| 24 |
+var ( |
|
| 25 |
+ timeMu sync.Mutex |
|
| 26 |
+ lasttime uint64 // last time we returned |
|
| 27 |
+ clockSeq uint16 // clock sequence for this run |
|
| 28 |
+ |
|
| 29 |
+ timeNow = time.Now // for testing |
|
| 30 |
+) |
|
| 31 |
+ |
|
| 32 |
+// UnixTime converts t the number of seconds and nanoseconds using the Unix |
|
| 33 |
+// epoch of 1 Jan 1970. |
|
| 34 |
+func (t Time) UnixTime() (sec, nsec int64) {
|
|
| 35 |
+ sec = int64(t - g1582ns100) |
|
| 36 |
+ nsec = (sec % 10000000) * 100 |
|
| 37 |
+ sec /= 10000000 |
|
| 38 |
+ return sec, nsec |
|
| 39 |
+} |
|
| 40 |
+ |
|
| 41 |
+// GetTime returns the current Time (100s of nanoseconds since 15 Oct 1582) and |
|
| 42 |
+// clock sequence as well as adjusting the clock sequence as needed. An error |
|
| 43 |
+// is returned if the current time cannot be determined. |
|
| 44 |
+func GetTime() (Time, uint16, error) {
|
|
| 45 |
+ defer timeMu.Unlock() |
|
| 46 |
+ timeMu.Lock() |
|
| 47 |
+ return getTime() |
|
| 48 |
+} |
|
| 49 |
+ |
|
| 50 |
+func getTime() (Time, uint16, error) {
|
|
| 51 |
+ t := timeNow() |
|
| 52 |
+ |
|
| 53 |
+ // If we don't have a clock sequence already, set one. |
|
| 54 |
+ if clockSeq == 0 {
|
|
| 55 |
+ setClockSequence(-1) |
|
| 56 |
+ } |
|
| 57 |
+ now := uint64(t.UnixNano()/100) + g1582ns100 |
|
| 58 |
+ |
|
| 59 |
+ // If time has gone backwards with this clock sequence then we |
|
| 60 |
+ // increment the clock sequence |
|
| 61 |
+ if now <= lasttime {
|
|
| 62 |
+ clockSeq = ((clockSeq + 1) & 0x3fff) | 0x8000 |
|
| 63 |
+ } |
|
| 64 |
+ lasttime = now |
|
| 65 |
+ return Time(now), clockSeq, nil |
|
| 66 |
+} |
|
| 67 |
+ |
|
| 68 |
+// ClockSequence returns the current clock sequence, generating one if not |
|
| 69 |
+// already set. The clock sequence is only used for Version 1 UUIDs. |
|
| 70 |
+// |
|
| 71 |
+// The uuid package does not use global static storage for the clock sequence or |
|
| 72 |
+// the last time a UUID was generated. Unless SetClockSequence is used, a new |
|
| 73 |
+// random clock sequence is generated the first time a clock sequence is |
|
| 74 |
+// requested by ClockSequence, GetTime, or NewUUID. (section 4.2.1.1) |
|
| 75 |
+func ClockSequence() int {
|
|
| 76 |
+ defer timeMu.Unlock() |
|
| 77 |
+ timeMu.Lock() |
|
| 78 |
+ return clockSequence() |
|
| 79 |
+} |
|
| 80 |
+ |
|
| 81 |
+func clockSequence() int {
|
|
| 82 |
+ if clockSeq == 0 {
|
|
| 83 |
+ setClockSequence(-1) |
|
| 84 |
+ } |
|
| 85 |
+ return int(clockSeq & 0x3fff) |
|
| 86 |
+} |
|
| 87 |
+ |
|
| 88 |
+// SetClockSequence sets the clock sequence to the lower 14 bits of seq. Setting to |
|
| 89 |
+// -1 causes a new sequence to be generated. |
|
| 90 |
+func SetClockSequence(seq int) {
|
|
| 91 |
+ defer timeMu.Unlock() |
|
| 92 |
+ timeMu.Lock() |
|
| 93 |
+ setClockSequence(seq) |
|
| 94 |
+} |
|
| 95 |
+ |
|
| 96 |
+func setClockSequence(seq int) {
|
|
| 97 |
+ if seq == -1 {
|
|
| 98 |
+ var b [2]byte |
|
| 99 |
+ randomBits(b[:]) // clock sequence |
|
| 100 |
+ seq = int(b[0])<<8 | int(b[1]) |
|
| 101 |
+ } |
|
| 102 |
+ oldSeq := clockSeq |
|
| 103 |
+ clockSeq = uint16(seq&0x3fff) | 0x8000 // Set our variant |
|
| 104 |
+ if oldSeq != clockSeq {
|
|
| 105 |
+ lasttime = 0 |
|
| 106 |
+ } |
|
| 107 |
+} |
|
| 108 |
+ |
|
| 109 |
+// Time returns the time in 100s of nanoseconds since 15 Oct 1582 encoded in |
|
| 110 |
+// uuid. The time is only defined for version 1 and 2 UUIDs. |
|
| 111 |
+func (uuid UUID) Time() Time {
|
|
| 112 |
+ time := int64(binary.BigEndian.Uint32(uuid[0:4])) |
|
| 113 |
+ time |= int64(binary.BigEndian.Uint16(uuid[4:6])) << 32 |
|
| 114 |
+ time |= int64(binary.BigEndian.Uint16(uuid[6:8])&0xfff) << 48 |
|
| 115 |
+ return Time(time) |
|
| 116 |
+} |
|
| 117 |
+ |
|
| 118 |
+// ClockSequence returns the clock sequence encoded in uuid. |
|
| 119 |
+// The clock sequence is only well defined for version 1 and 2 UUIDs. |
|
| 120 |
+func (uuid UUID) ClockSequence() int {
|
|
| 121 |
+ return int(binary.BigEndian.Uint16(uuid[8:10])) & 0x3fff |
|
| 122 |
+} |
| 0 | 123 |
new file mode 100644 |
| ... | ... |
@@ -0,0 +1,43 @@ |
| 0 |
+// Copyright 2016 Google Inc. All rights reserved. |
|
| 1 |
+// Use of this source code is governed by a BSD-style |
|
| 2 |
+// license that can be found in the LICENSE file. |
|
| 3 |
+ |
|
| 4 |
+package uuid |
|
| 5 |
+ |
|
| 6 |
+import ( |
|
| 7 |
+ "io" |
|
| 8 |
+) |
|
| 9 |
+ |
|
| 10 |
+// randomBits completely fills slice b with random data. |
|
| 11 |
+func randomBits(b []byte) {
|
|
| 12 |
+ if _, err := io.ReadFull(rander, b); err != nil {
|
|
| 13 |
+ panic(err.Error()) // rand should never fail |
|
| 14 |
+ } |
|
| 15 |
+} |
|
| 16 |
+ |
|
| 17 |
+// xvalues returns the value of a byte as a hexadecimal digit or 255. |
|
| 18 |
+var xvalues = [256]byte{
|
|
| 19 |
+ 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, |
|
| 20 |
+ 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, |
|
| 21 |
+ 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, |
|
| 22 |
+ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 255, 255, 255, 255, 255, 255, |
|
| 23 |
+ 255, 10, 11, 12, 13, 14, 15, 255, 255, 255, 255, 255, 255, 255, 255, 255, |
|
| 24 |
+ 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, |
|
| 25 |
+ 255, 10, 11, 12, 13, 14, 15, 255, 255, 255, 255, 255, 255, 255, 255, 255, |
|
| 26 |
+ 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, |
|
| 27 |
+ 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, |
|
| 28 |
+ 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, |
|
| 29 |
+ 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, |
|
| 30 |
+ 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, |
|
| 31 |
+ 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, |
|
| 32 |
+ 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, |
|
| 33 |
+ 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, |
|
| 34 |
+ 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, |
|
| 35 |
+} |
|
| 36 |
+ |
|
| 37 |
+// xtob converts hex characters x1 and x2 into a byte. |
|
| 38 |
+func xtob(x1, x2 byte) (byte, bool) {
|
|
| 39 |
+ b1 := xvalues[x1] |
|
| 40 |
+ b2 := xvalues[x2] |
|
| 41 |
+ return (b1 << 4) | b2, b1 != 255 && b2 != 255 |
|
| 42 |
+} |
| 0 | 43 |
new file mode 100644 |
| ... | ... |
@@ -0,0 +1,245 @@ |
| 0 |
+// Copyright 2018 Google Inc. All rights reserved. |
|
| 1 |
+// Use of this source code is governed by a BSD-style |
|
| 2 |
+// license that can be found in the LICENSE file. |
|
| 3 |
+ |
|
| 4 |
+package uuid |
|
| 5 |
+ |
|
| 6 |
+import ( |
|
| 7 |
+ "bytes" |
|
| 8 |
+ "crypto/rand" |
|
| 9 |
+ "encoding/hex" |
|
| 10 |
+ "errors" |
|
| 11 |
+ "fmt" |
|
| 12 |
+ "io" |
|
| 13 |
+ "strings" |
|
| 14 |
+) |
|
| 15 |
+ |
|
| 16 |
+// A UUID is a 128 bit (16 byte) Universal Unique IDentifier as defined in RFC |
|
| 17 |
+// 4122. |
|
| 18 |
+type UUID [16]byte |
|
| 19 |
+ |
|
| 20 |
+// A Version represents a UUID's version. |
|
| 21 |
+type Version byte |
|
| 22 |
+ |
|
| 23 |
+// A Variant represents a UUID's variant. |
|
| 24 |
+type Variant byte |
|
| 25 |
+ |
|
| 26 |
+// Constants returned by Variant. |
|
| 27 |
+const ( |
|
| 28 |
+ Invalid = Variant(iota) // Invalid UUID |
|
| 29 |
+ RFC4122 // The variant specified in RFC4122 |
|
| 30 |
+ Reserved // Reserved, NCS backward compatibility. |
|
| 31 |
+ Microsoft // Reserved, Microsoft Corporation backward compatibility. |
|
| 32 |
+ Future // Reserved for future definition. |
|
| 33 |
+) |
|
| 34 |
+ |
|
| 35 |
+var rander = rand.Reader // random function |
|
| 36 |
+ |
|
| 37 |
+// Parse decodes s into a UUID or returns an error. Both the standard UUID |
|
| 38 |
+// forms of xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx and |
|
| 39 |
+// urn:uuid:xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx are decoded as well as the |
|
| 40 |
+// Microsoft encoding {xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx} and the raw hex
|
|
| 41 |
+// encoding: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx. |
|
| 42 |
+func Parse(s string) (UUID, error) {
|
|
| 43 |
+ var uuid UUID |
|
| 44 |
+ switch len(s) {
|
|
| 45 |
+ // xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx |
|
| 46 |
+ case 36: |
|
| 47 |
+ |
|
| 48 |
+ // urn:uuid:xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx |
|
| 49 |
+ case 36 + 9: |
|
| 50 |
+ if strings.ToLower(s[:9]) != "urn:uuid:" {
|
|
| 51 |
+ return uuid, fmt.Errorf("invalid urn prefix: %q", s[:9])
|
|
| 52 |
+ } |
|
| 53 |
+ s = s[9:] |
|
| 54 |
+ |
|
| 55 |
+ // {xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx}
|
|
| 56 |
+ case 36 + 2: |
|
| 57 |
+ s = s[1:] |
|
| 58 |
+ |
|
| 59 |
+ // xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx |
|
| 60 |
+ case 32: |
|
| 61 |
+ var ok bool |
|
| 62 |
+ for i := range uuid {
|
|
| 63 |
+ uuid[i], ok = xtob(s[i*2], s[i*2+1]) |
|
| 64 |
+ if !ok {
|
|
| 65 |
+ return uuid, errors.New("invalid UUID format")
|
|
| 66 |
+ } |
|
| 67 |
+ } |
|
| 68 |
+ return uuid, nil |
|
| 69 |
+ default: |
|
| 70 |
+ return uuid, fmt.Errorf("invalid UUID length: %d", len(s))
|
|
| 71 |
+ } |
|
| 72 |
+ // s is now at least 36 bytes long |
|
| 73 |
+ // it must be of the form xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx |
|
| 74 |
+ if s[8] != '-' || s[13] != '-' || s[18] != '-' || s[23] != '-' {
|
|
| 75 |
+ return uuid, errors.New("invalid UUID format")
|
|
| 76 |
+ } |
|
| 77 |
+ for i, x := range [16]int{
|
|
| 78 |
+ 0, 2, 4, 6, |
|
| 79 |
+ 9, 11, |
|
| 80 |
+ 14, 16, |
|
| 81 |
+ 19, 21, |
|
| 82 |
+ 24, 26, 28, 30, 32, 34} {
|
|
| 83 |
+ v, ok := xtob(s[x], s[x+1]) |
|
| 84 |
+ if !ok {
|
|
| 85 |
+ return uuid, errors.New("invalid UUID format")
|
|
| 86 |
+ } |
|
| 87 |
+ uuid[i] = v |
|
| 88 |
+ } |
|
| 89 |
+ return uuid, nil |
|
| 90 |
+} |
|
| 91 |
+ |
|
| 92 |
+// ParseBytes is like Parse, except it parses a byte slice instead of a string. |
|
| 93 |
+func ParseBytes(b []byte) (UUID, error) {
|
|
| 94 |
+ var uuid UUID |
|
| 95 |
+ switch len(b) {
|
|
| 96 |
+ case 36: // xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx |
|
| 97 |
+ case 36 + 9: // urn:uuid:xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx |
|
| 98 |
+ if !bytes.Equal(bytes.ToLower(b[:9]), []byte("urn:uuid:")) {
|
|
| 99 |
+ return uuid, fmt.Errorf("invalid urn prefix: %q", b[:9])
|
|
| 100 |
+ } |
|
| 101 |
+ b = b[9:] |
|
| 102 |
+ case 36 + 2: // {xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx}
|
|
| 103 |
+ b = b[1:] |
|
| 104 |
+ case 32: // xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx |
|
| 105 |
+ var ok bool |
|
| 106 |
+ for i := 0; i < 32; i += 2 {
|
|
| 107 |
+ uuid[i/2], ok = xtob(b[i], b[i+1]) |
|
| 108 |
+ if !ok {
|
|
| 109 |
+ return uuid, errors.New("invalid UUID format")
|
|
| 110 |
+ } |
|
| 111 |
+ } |
|
| 112 |
+ return uuid, nil |
|
| 113 |
+ default: |
|
| 114 |
+ return uuid, fmt.Errorf("invalid UUID length: %d", len(b))
|
|
| 115 |
+ } |
|
| 116 |
+ // s is now at least 36 bytes long |
|
| 117 |
+ // it must be of the form xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx |
|
| 118 |
+ if b[8] != '-' || b[13] != '-' || b[18] != '-' || b[23] != '-' {
|
|
| 119 |
+ return uuid, errors.New("invalid UUID format")
|
|
| 120 |
+ } |
|
| 121 |
+ for i, x := range [16]int{
|
|
| 122 |
+ 0, 2, 4, 6, |
|
| 123 |
+ 9, 11, |
|
| 124 |
+ 14, 16, |
|
| 125 |
+ 19, 21, |
|
| 126 |
+ 24, 26, 28, 30, 32, 34} {
|
|
| 127 |
+ v, ok := xtob(b[x], b[x+1]) |
|
| 128 |
+ if !ok {
|
|
| 129 |
+ return uuid, errors.New("invalid UUID format")
|
|
| 130 |
+ } |
|
| 131 |
+ uuid[i] = v |
|
| 132 |
+ } |
|
| 133 |
+ return uuid, nil |
|
| 134 |
+} |
|
| 135 |
+ |
|
| 136 |
+// MustParse is like Parse but panics if the string cannot be parsed. |
|
| 137 |
+// It simplifies safe initialization of global variables holding compiled UUIDs. |
|
| 138 |
+func MustParse(s string) UUID {
|
|
| 139 |
+ uuid, err := Parse(s) |
|
| 140 |
+ if err != nil {
|
|
| 141 |
+ panic(`uuid: Parse(` + s + `): ` + err.Error()) |
|
| 142 |
+ } |
|
| 143 |
+ return uuid |
|
| 144 |
+} |
|
| 145 |
+ |
|
| 146 |
+// FromBytes creates a new UUID from a byte slice. Returns an error if the slice |
|
| 147 |
+// does not have a length of 16. The bytes are copied from the slice. |
|
| 148 |
+func FromBytes(b []byte) (uuid UUID, err error) {
|
|
| 149 |
+ err = uuid.UnmarshalBinary(b) |
|
| 150 |
+ return uuid, err |
|
| 151 |
+} |
|
| 152 |
+ |
|
| 153 |
+// Must returns uuid if err is nil and panics otherwise. |
|
| 154 |
+func Must(uuid UUID, err error) UUID {
|
|
| 155 |
+ if err != nil {
|
|
| 156 |
+ panic(err) |
|
| 157 |
+ } |
|
| 158 |
+ return uuid |
|
| 159 |
+} |
|
| 160 |
+ |
|
| 161 |
+// String returns the string form of uuid, xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx |
|
| 162 |
+// , or "" if uuid is invalid. |
|
| 163 |
+func (uuid UUID) String() string {
|
|
| 164 |
+ var buf [36]byte |
|
| 165 |
+ encodeHex(buf[:], uuid) |
|
| 166 |
+ return string(buf[:]) |
|
| 167 |
+} |
|
| 168 |
+ |
|
| 169 |
+// URN returns the RFC 2141 URN form of uuid, |
|
| 170 |
+// urn:uuid:xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx, or "" if uuid is invalid. |
|
| 171 |
+func (uuid UUID) URN() string {
|
|
| 172 |
+ var buf [36 + 9]byte |
|
| 173 |
+ copy(buf[:], "urn:uuid:") |
|
| 174 |
+ encodeHex(buf[9:], uuid) |
|
| 175 |
+ return string(buf[:]) |
|
| 176 |
+} |
|
| 177 |
+ |
|
| 178 |
+func encodeHex(dst []byte, uuid UUID) {
|
|
| 179 |
+ hex.Encode(dst, uuid[:4]) |
|
| 180 |
+ dst[8] = '-' |
|
| 181 |
+ hex.Encode(dst[9:13], uuid[4:6]) |
|
| 182 |
+ dst[13] = '-' |
|
| 183 |
+ hex.Encode(dst[14:18], uuid[6:8]) |
|
| 184 |
+ dst[18] = '-' |
|
| 185 |
+ hex.Encode(dst[19:23], uuid[8:10]) |
|
| 186 |
+ dst[23] = '-' |
|
| 187 |
+ hex.Encode(dst[24:], uuid[10:]) |
|
| 188 |
+} |
|
| 189 |
+ |
|
| 190 |
+// Variant returns the variant encoded in uuid. |
|
| 191 |
+func (uuid UUID) Variant() Variant {
|
|
| 192 |
+ switch {
|
|
| 193 |
+ case (uuid[8] & 0xc0) == 0x80: |
|
| 194 |
+ return RFC4122 |
|
| 195 |
+ case (uuid[8] & 0xe0) == 0xc0: |
|
| 196 |
+ return Microsoft |
|
| 197 |
+ case (uuid[8] & 0xe0) == 0xe0: |
|
| 198 |
+ return Future |
|
| 199 |
+ default: |
|
| 200 |
+ return Reserved |
|
| 201 |
+ } |
|
| 202 |
+} |
|
| 203 |
+ |
|
| 204 |
+// Version returns the version of uuid. |
|
| 205 |
+func (uuid UUID) Version() Version {
|
|
| 206 |
+ return Version(uuid[6] >> 4) |
|
| 207 |
+} |
|
| 208 |
+ |
|
| 209 |
+func (v Version) String() string {
|
|
| 210 |
+ if v > 15 {
|
|
| 211 |
+ return fmt.Sprintf("BAD_VERSION_%d", v)
|
|
| 212 |
+ } |
|
| 213 |
+ return fmt.Sprintf("VERSION_%d", v)
|
|
| 214 |
+} |
|
| 215 |
+ |
|
| 216 |
+func (v Variant) String() string {
|
|
| 217 |
+ switch v {
|
|
| 218 |
+ case RFC4122: |
|
| 219 |
+ return "RFC4122" |
|
| 220 |
+ case Reserved: |
|
| 221 |
+ return "Reserved" |
|
| 222 |
+ case Microsoft: |
|
| 223 |
+ return "Microsoft" |
|
| 224 |
+ case Future: |
|
| 225 |
+ return "Future" |
|
| 226 |
+ case Invalid: |
|
| 227 |
+ return "Invalid" |
|
| 228 |
+ } |
|
| 229 |
+ return fmt.Sprintf("BadVariant%d", int(v))
|
|
| 230 |
+} |
|
| 231 |
+ |
|
| 232 |
+// SetRand sets the random number generator to r, which implements io.Reader. |
|
| 233 |
+// If r.Read returns an error when the package requests random data then |
|
| 234 |
+// a panic will be issued. |
|
| 235 |
+// |
|
| 236 |
+// Calling SetRand with nil sets the random number generator to the default |
|
| 237 |
+// generator. |
|
| 238 |
+func SetRand(r io.Reader) {
|
|
| 239 |
+ if r == nil {
|
|
| 240 |
+ rander = rand.Reader |
|
| 241 |
+ return |
|
| 242 |
+ } |
|
| 243 |
+ rander = r |
|
| 244 |
+} |
| 0 | 245 |
new file mode 100644 |
| ... | ... |
@@ -0,0 +1,44 @@ |
| 0 |
+// Copyright 2016 Google Inc. All rights reserved. |
|
| 1 |
+// Use of this source code is governed by a BSD-style |
|
| 2 |
+// license that can be found in the LICENSE file. |
|
| 3 |
+ |
|
| 4 |
+package uuid |
|
| 5 |
+ |
|
| 6 |
+import ( |
|
| 7 |
+ "encoding/binary" |
|
| 8 |
+) |
|
| 9 |
+ |
|
| 10 |
+// NewUUID returns a Version 1 UUID based on the current NodeID and clock |
|
| 11 |
+// sequence, and the current time. If the NodeID has not been set by SetNodeID |
|
| 12 |
+// or SetNodeInterface then it will be set automatically. If the NodeID cannot |
|
| 13 |
+// be set NewUUID returns nil. If clock sequence has not been set by |
|
| 14 |
+// SetClockSequence then it will be set automatically. If GetTime fails to |
|
| 15 |
+// return the current NewUUID returns nil and an error. |
|
| 16 |
+// |
|
| 17 |
+// In most cases, New should be used. |
|
| 18 |
+func NewUUID() (UUID, error) {
|
|
| 19 |
+ nodeMu.Lock() |
|
| 20 |
+ if nodeID == zeroID {
|
|
| 21 |
+ setNodeInterface("")
|
|
| 22 |
+ } |
|
| 23 |
+ nodeMu.Unlock() |
|
| 24 |
+ |
|
| 25 |
+ var uuid UUID |
|
| 26 |
+ now, seq, err := GetTime() |
|
| 27 |
+ if err != nil {
|
|
| 28 |
+ return uuid, err |
|
| 29 |
+ } |
|
| 30 |
+ |
|
| 31 |
+ timeLow := uint32(now & 0xffffffff) |
|
| 32 |
+ timeMid := uint16((now >> 32) & 0xffff) |
|
| 33 |
+ timeHi := uint16((now >> 48) & 0x0fff) |
|
| 34 |
+ timeHi |= 0x1000 // Version 1 |
|
| 35 |
+ |
|
| 36 |
+ binary.BigEndian.PutUint32(uuid[0:], timeLow) |
|
| 37 |
+ binary.BigEndian.PutUint16(uuid[4:], timeMid) |
|
| 38 |
+ binary.BigEndian.PutUint16(uuid[6:], timeHi) |
|
| 39 |
+ binary.BigEndian.PutUint16(uuid[8:], seq) |
|
| 40 |
+ copy(uuid[10:], nodeID[:]) |
|
| 41 |
+ |
|
| 42 |
+ return uuid, nil |
|
| 43 |
+} |
| 0 | 44 |
new file mode 100644 |
| ... | ... |
@@ -0,0 +1,38 @@ |
| 0 |
+// Copyright 2016 Google Inc. All rights reserved. |
|
| 1 |
+// Use of this source code is governed by a BSD-style |
|
| 2 |
+// license that can be found in the LICENSE file. |
|
| 3 |
+ |
|
| 4 |
+package uuid |
|
| 5 |
+ |
|
| 6 |
+import "io" |
|
| 7 |
+ |
|
| 8 |
+// New creates a new random UUID or panics. New is equivalent to |
|
| 9 |
+// the expression |
|
| 10 |
+// |
|
| 11 |
+// uuid.Must(uuid.NewRandom()) |
|
| 12 |
+func New() UUID {
|
|
| 13 |
+ return Must(NewRandom()) |
|
| 14 |
+} |
|
| 15 |
+ |
|
| 16 |
+// NewRandom returns a Random (Version 4) UUID. |
|
| 17 |
+// |
|
| 18 |
+// The strength of the UUIDs is based on the strength of the crypto/rand |
|
| 19 |
+// package. |
|
| 20 |
+// |
|
| 21 |
+// A note about uniqueness derived from the UUID Wikipedia entry: |
|
| 22 |
+// |
|
| 23 |
+// Randomly generated UUIDs have 122 random bits. One's annual risk of being |
|
| 24 |
+// hit by a meteorite is estimated to be one chance in 17 billion, that |
|
| 25 |
+// means the probability is about 0.00000000006 (6 × 10−11), |
|
| 26 |
+// equivalent to the odds of creating a few tens of trillions of UUIDs in a |
|
| 27 |
+// year and having one duplicate. |
|
| 28 |
+func NewRandom() (UUID, error) {
|
|
| 29 |
+ var uuid UUID |
|
| 30 |
+ _, err := io.ReadFull(rander, uuid[:]) |
|
| 31 |
+ if err != nil {
|
|
| 32 |
+ return Nil, err |
|
| 33 |
+ } |
|
| 34 |
+ uuid[6] = (uuid[6] & 0x0f) | 0x40 // Version 4 |
|
| 35 |
+ uuid[8] = (uuid[8] & 0x3f) | 0x80 // Variant is 10 |
|
| 36 |
+ return uuid, nil |
|
| 37 |
+} |