blob: 7c643cf0a38e84cffd67750a8a14686e86dfdafb [file] [log] [blame]
Matthias Andreas Benkard832a54e2019-01-29 09:27:38 +01001// Copyright 2011 Google Inc. All rights reserved.
2// Use of this source code is governed by a BSD-style
3// license that can be found in the LICENSE file.
4
5package uuid
6
7import (
8 "bytes"
9 "crypto/rand"
10 "encoding/hex"
11 "fmt"
12 "io"
13 "strings"
14)
15
16// Array is a pass-by-value UUID that can be used as an effecient key in a map.
17type Array [16]byte
18
19// UUID converts uuid into a slice.
20func (uuid Array) UUID() UUID {
21 return uuid[:]
22}
23
24// String returns the string representation of uuid,
25// xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx.
26func (uuid Array) String() string {
27 return uuid.UUID().String()
28}
29
30// A UUID is a 128 bit (16 byte) Universal Unique IDentifier as defined in RFC
31// 4122.
32type UUID []byte
33
34// A Version represents a UUIDs version.
35type Version byte
36
37// A Variant represents a UUIDs variant.
38type Variant byte
39
40// Constants returned by Variant.
41const (
42 Invalid = Variant(iota) // Invalid UUID
43 RFC4122 // The variant specified in RFC4122
44 Reserved // Reserved, NCS backward compatibility.
45 Microsoft // Reserved, Microsoft Corporation backward compatibility.
46 Future // Reserved for future definition.
47)
48
49var rander = rand.Reader // random function
50
51// New returns a new random (version 4) UUID as a string. It is a convenience
52// function for NewRandom().String().
53func New() string {
54 return NewRandom().String()
55}
56
57// Parse decodes s into a UUID or returns nil. Both the UUID form of
58// xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx and
59// urn:uuid:xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx are decoded.
60func Parse(s string) UUID {
61 if len(s) == 36+9 {
62 if strings.ToLower(s[:9]) != "urn:uuid:" {
63 return nil
64 }
65 s = s[9:]
66 } else if len(s) != 36 {
67 return nil
68 }
69 if s[8] != '-' || s[13] != '-' || s[18] != '-' || s[23] != '-' {
70 return nil
71 }
72 var uuid [16]byte
73 for i, x := range [16]int{
74 0, 2, 4, 6,
75 9, 11,
76 14, 16,
77 19, 21,
78 24, 26, 28, 30, 32, 34} {
79 if v, ok := xtob(s[x:]); !ok {
80 return nil
81 } else {
82 uuid[i] = v
83 }
84 }
85 return uuid[:]
86}
87
88// Equal returns true if uuid1 and uuid2 are equal.
89func Equal(uuid1, uuid2 UUID) bool {
90 return bytes.Equal(uuid1, uuid2)
91}
92
93// Array returns an array representation of uuid that can be used as a map key.
94// Array panics if uuid is not valid.
95func (uuid UUID) Array() Array {
96 if len(uuid) != 16 {
97 panic("invalid uuid")
98 }
99 var a Array
100 copy(a[:], uuid)
101 return a
102}
103
104// String returns the string form of uuid, xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
105// , or "" if uuid is invalid.
106func (uuid UUID) String() string {
107 if len(uuid) != 16 {
108 return ""
109 }
110 var buf [36]byte
111 encodeHex(buf[:], uuid)
112 return string(buf[:])
113}
114
115// URN returns the RFC 2141 URN form of uuid,
116// urn:uuid:xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx, or "" if uuid is invalid.
117func (uuid UUID) URN() string {
118 if len(uuid) != 16 {
119 return ""
120 }
121 var buf [36 + 9]byte
122 copy(buf[:], "urn:uuid:")
123 encodeHex(buf[9:], uuid)
124 return string(buf[:])
125}
126
127func encodeHex(dst []byte, uuid UUID) {
128 hex.Encode(dst[:], uuid[:4])
129 dst[8] = '-'
130 hex.Encode(dst[9:13], uuid[4:6])
131 dst[13] = '-'
132 hex.Encode(dst[14:18], uuid[6:8])
133 dst[18] = '-'
134 hex.Encode(dst[19:23], uuid[8:10])
135 dst[23] = '-'
136 hex.Encode(dst[24:], uuid[10:])
137}
138
139// Variant returns the variant encoded in uuid. It returns Invalid if
140// uuid is invalid.
141func (uuid UUID) Variant() Variant {
142 if len(uuid) != 16 {
143 return Invalid
144 }
145 switch {
146 case (uuid[8] & 0xc0) == 0x80:
147 return RFC4122
148 case (uuid[8] & 0xe0) == 0xc0:
149 return Microsoft
150 case (uuid[8] & 0xe0) == 0xe0:
151 return Future
152 default:
153 return Reserved
154 }
155}
156
157// Version returns the version of uuid. It returns false if uuid is not
158// valid.
159func (uuid UUID) Version() (Version, bool) {
160 if len(uuid) != 16 {
161 return 0, false
162 }
163 return Version(uuid[6] >> 4), true
164}
165
166func (v Version) String() string {
167 if v > 15 {
168 return fmt.Sprintf("BAD_VERSION_%d", v)
169 }
170 return fmt.Sprintf("VERSION_%d", v)
171}
172
173func (v Variant) String() string {
174 switch v {
175 case RFC4122:
176 return "RFC4122"
177 case Reserved:
178 return "Reserved"
179 case Microsoft:
180 return "Microsoft"
181 case Future:
182 return "Future"
183 case Invalid:
184 return "Invalid"
185 }
186 return fmt.Sprintf("BadVariant%d", int(v))
187}
188
189// SetRand sets the random number generator to r, which implements io.Reader.
190// If r.Read returns an error when the package requests random data then
191// a panic will be issued.
192//
193// Calling SetRand with nil sets the random number generator to the default
194// generator.
195func SetRand(r io.Reader) {
196 if r == nil {
197 rander = rand.Reader
198 return
199 }
200 rander = r
201}