uuid.go 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. package uuid
  2. import (
  3. "bytes"
  4. "crypto/md5"
  5. "crypto/rand"
  6. "encoding/hex"
  7. "errors"
  8. )
  9. var (
  10. byteGroups = []int{8, 4, 4, 4, 12}
  11. InvalidID = errors.New("Invalid ID.")
  12. )
  13. type UUID struct {
  14. byteValue []byte
  15. stringValue string
  16. }
  17. func (this *UUID) String() string {
  18. return this.stringValue
  19. }
  20. func (this *UUID) Bytes() []byte {
  21. return this.byteValue
  22. }
  23. func (this *UUID) Equals(another *UUID) bool {
  24. if this == nil && another == nil {
  25. return true
  26. }
  27. if this == nil || another == nil {
  28. return false
  29. }
  30. return bytes.Equal(this.Bytes(), another.Bytes())
  31. }
  32. // Next generates a deterministic random UUID based on this UUID.
  33. func (this *UUID) Next() *UUID {
  34. md5hash := md5.New()
  35. md5hash.Write(this.Bytes())
  36. md5hash.Write([]byte("16167dc8-16b6-4e6d-b8bb-65dd68113a81"))
  37. for {
  38. newid, _ := ParseBytes(md5hash.Sum(nil))
  39. if !newid.Equals(this) {
  40. return newid
  41. }
  42. md5hash.Write([]byte("533eff8a-4113-4b10-b5ce-0f5d76b98cd2"))
  43. }
  44. }
  45. func bytesToString(bytes []byte) string {
  46. result := hex.EncodeToString(bytes[0 : byteGroups[0]/2])
  47. start := byteGroups[0] / 2
  48. for i := 1; i < len(byteGroups); i++ {
  49. nBytes := byteGroups[i] / 2
  50. result += "-"
  51. result += hex.EncodeToString(bytes[start : start+nBytes])
  52. start += nBytes
  53. }
  54. return result
  55. }
  56. func New() *UUID {
  57. bytes := make([]byte, 16)
  58. rand.Read(bytes)
  59. uuid, _ := ParseBytes(bytes)
  60. return uuid
  61. }
  62. func ParseBytes(bytes []byte) (*UUID, error) {
  63. if len(bytes) != 16 {
  64. return nil, InvalidID
  65. }
  66. return &UUID{
  67. byteValue: bytes,
  68. stringValue: bytesToString(bytes),
  69. }, nil
  70. }
  71. func ParseString(str string) (*UUID, error) {
  72. text := []byte(str)
  73. if len(text) < 32 {
  74. return nil, InvalidID
  75. }
  76. uuid := &UUID{
  77. byteValue: make([]byte, 16),
  78. stringValue: str,
  79. }
  80. b := uuid.byteValue[:]
  81. for _, byteGroup := range byteGroups {
  82. if text[0] == '-' {
  83. text = text[1:]
  84. }
  85. _, err := hex.Decode(b[:byteGroup/2], text[:byteGroup])
  86. if err != nil {
  87. return nil, err
  88. }
  89. text = text[byteGroup:]
  90. b = b[byteGroup/2:]
  91. }
  92. return uuid, nil
  93. }