uuid.go 4.5 KB

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