uuid.go 1.8 KB

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