2017-09-29 16:58:48 +00:00
|
|
|
package uuid
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2022-08-30 14:59:25 +00:00
|
|
|
|
|
|
|
"github.com/hashicorp/nomad/helper/crypto"
|
2017-09-29 16:58:48 +00:00
|
|
|
)
|
|
|
|
|
2021-01-04 22:06:47 +00:00
|
|
|
// Generate is used to generate a random UUID.
|
2017-09-29 16:58:48 +00:00
|
|
|
func Generate() string {
|
2022-08-30 14:59:25 +00:00
|
|
|
buf, err := crypto.Bytes(16)
|
|
|
|
if err != nil {
|
2017-09-29 16:58:48 +00:00
|
|
|
panic(fmt.Errorf("failed to read random bytes: %v", err))
|
|
|
|
}
|
|
|
|
|
|
|
|
return fmt.Sprintf("%08x-%04x-%04x-%04x-%12x",
|
|
|
|
buf[0:4],
|
|
|
|
buf[4:6],
|
|
|
|
buf[6:8],
|
|
|
|
buf[8:10],
|
|
|
|
buf[10:16])
|
|
|
|
}
|
2021-01-04 22:06:47 +00:00
|
|
|
|
|
|
|
// Short is used to generate the first 8 characters of a UUID.
|
|
|
|
func Short() string {
|
|
|
|
return Generate()[0:8]
|
|
|
|
}
|