uuid.go 3.7 KB

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