version4.go 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. // Copyright 2016 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 "io"
  6. // New creates a new random UUID or panics. New is equivalent to
  7. // the expression
  8. //
  9. // uuid.Must(uuid.NewRandom())
  10. func New() UUID {
  11. return Must(NewRandom())
  12. }
  13. // NewString creates a new random UUID and returns it as a string or panics.
  14. // NewString is equivalent to the expression
  15. //
  16. // uuid.New().String()
  17. func NewString() string {
  18. return Must(NewRandom()).String()
  19. }
  20. // NewRandom returns a Random (Version 4) UUID.
  21. //
  22. // The strength of the UUIDs is based on the strength of the crypto/rand
  23. // package.
  24. //
  25. // Uses the randomness pool if it was enabled with EnableRandPool.
  26. //
  27. // A note about uniqueness derived from the UUID Wikipedia entry:
  28. //
  29. // Randomly generated UUIDs have 122 random bits. One's annual risk of being
  30. // hit by a meteorite is estimated to be one chance in 17 billion, that
  31. // means the probability is about 0.00000000006 (6 × 10−11),
  32. // equivalent to the odds of creating a few tens of trillions of UUIDs in a
  33. // year and having one duplicate.
  34. func NewRandom() (UUID, error) {
  35. if !poolEnabled {
  36. return NewRandomFromReader(rander)
  37. }
  38. return newRandomFromPool()
  39. }
  40. // NewRandomFromReader returns a UUID based on bytes read from a given io.Reader.
  41. func NewRandomFromReader(r io.Reader) (UUID, error) {
  42. var uuid UUID
  43. _, err := io.ReadFull(r, uuid[:])
  44. if err != nil {
  45. return Nil, err
  46. }
  47. uuid[6] = (uuid[6] & 0x0f) | 0x40 // Version 4
  48. uuid[8] = (uuid[8] & 0x3f) | 0x80 // Variant is 10
  49. return uuid, nil
  50. }
  51. func newRandomFromPool() (UUID, error) {
  52. var uuid UUID
  53. poolMu.Lock()
  54. if poolPos == randPoolSize {
  55. _, err := io.ReadFull(rander, pool[:])
  56. if err != nil {
  57. poolMu.Unlock()
  58. return Nil, err
  59. }
  60. poolPos = 0
  61. }
  62. copy(uuid[:], pool[poolPos:(poolPos+16)])
  63. poolPos += 16
  64. poolMu.Unlock()
  65. uuid[6] = (uuid[6] & 0x0f) | 0x40 // Version 4
  66. uuid[8] = (uuid[8] & 0x3f) | 0x80 // Variant is 10
  67. return uuid, nil
  68. }