uuid.go 3.9 KB

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