github.com/mier85/go-sensor@v1.30.1-0.20220920111756-9bf41b3bc7e0/autoprofile/internal/uuid.go (about) 1 // (c) Copyright IBM Corp. 2021 2 // (c) Copyright Instana Inc. 2020 3 4 package internal 5 6 import ( 7 "crypto/sha1" 8 "encoding/hex" 9 "math/rand" 10 "strconv" 11 "sync" 12 "sync/atomic" 13 "time" 14 ) 15 16 var ( 17 randSource = rand.New(rand.NewSource(time.Now().UnixNano())) 18 randLock sync.Mutex 19 nextID int64 20 ) 21 22 // GenerateUUID generates a new UUID string 23 func GenerateUUID() string { 24 n := atomic.AddInt64(&nextID, 1) 25 26 uuid := strconv.FormatInt(time.Now().Unix(), 10) + 27 strconv.FormatInt(random(1000000000), 10) + 28 strconv.FormatInt(n, 10) 29 30 return sha1String(uuid) 31 } 32 33 func random(max int64) int64 { 34 randLock.Lock() 35 defer randLock.Unlock() 36 37 return randSource.Int63n(max) 38 } 39 40 func sha1String(s string) string { 41 sha1 := sha1.New() 42 sha1.Write([]byte(s)) 43 44 return hex.EncodeToString(sha1.Sum(nil)) 45 }