uuid.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. package rand
  2. import (
  3. "encoding/hex"
  4. "io"
  5. )
  6. const dash byte = '-'
  7. // UUIDIdempotencyToken provides a utility to get idempotency tokens in the
  8. // UUID format.
  9. type UUIDIdempotencyToken struct {
  10. uuid *UUID
  11. }
  12. // NewUUIDIdempotencyToken returns a idempotency token provider returning
  13. // tokens in the UUID random format using the reader provided.
  14. func NewUUIDIdempotencyToken(r io.Reader) *UUIDIdempotencyToken {
  15. return &UUIDIdempotencyToken{uuid: NewUUID(r)}
  16. }
  17. // GetIdempotencyToken returns a random UUID value for Idempotency token.
  18. func (u UUIDIdempotencyToken) GetIdempotencyToken() (string, error) {
  19. return u.uuid.GetUUID()
  20. }
  21. // UUID provides computing random UUID version 4 values from a random source
  22. // reader.
  23. type UUID struct {
  24. randSrc io.Reader
  25. }
  26. // NewUUID returns an initialized UUID value that can be used to retrieve
  27. // random UUID version 4 values.
  28. func NewUUID(r io.Reader) *UUID {
  29. return &UUID{randSrc: r}
  30. }
  31. // GetUUID returns a random UUID version 4 string representation sourced from the random reader the
  32. // UUID was created with. Returns an error if unable to compute the UUID.
  33. func (r *UUID) GetUUID() (string, error) {
  34. var b [16]byte
  35. if _, err := io.ReadFull(r.randSrc, b[:]); err != nil {
  36. return "", err
  37. }
  38. r.makeUUIDv4(b[:])
  39. return format(b), nil
  40. }
  41. // GetBytes returns a byte slice containing a random UUID version 4 sourced from the random reader the
  42. // UUID was created with. Returns an error if unable to compute the UUID.
  43. func (r *UUID) GetBytes() (u []byte, err error) {
  44. u = make([]byte, 16)
  45. if _, err = io.ReadFull(r.randSrc, u); err != nil {
  46. return u, err
  47. }
  48. r.makeUUIDv4(u)
  49. return u, nil
  50. }
  51. func (r *UUID) makeUUIDv4(u []byte) {
  52. // 13th character is "4"
  53. u[6] = (u[6] & 0x0f) | 0x40 // Version 4
  54. // 17th character is "8", "9", "a", or "b"
  55. u[8] = (u[8] & 0x3f) | 0x80 // Variant most significant bits are 10x where x can be either 1 or 0
  56. }
  57. // Format returns the canonical text representation of a UUID.
  58. // This implementation is optimized to not use fmt.
  59. // Example: 82e42f16-b6cc-4d5b-95f5-d403c4befd3d
  60. func format(u [16]byte) string {
  61. // https://en.wikipedia.org/wiki/Universally_unique_identifier#Version_4_.28random.29
  62. var scratch [36]byte
  63. hex.Encode(scratch[:8], u[0:4])
  64. scratch[8] = dash
  65. hex.Encode(scratch[9:13], u[4:6])
  66. scratch[13] = dash
  67. hex.Encode(scratch[14:18], u[6:8])
  68. scratch[18] = dash
  69. hex.Encode(scratch[19:23], u[8:10])
  70. scratch[23] = dash
  71. hex.Encode(scratch[24:], u[10:])
  72. return string(scratch[:])
  73. }