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