mirror of https://github.com/hashicorp/packer
parent
d094cb7c5c
commit
19b1bdee98
@ -1,32 +1,24 @@
|
|||||||
package uuid
|
package uuid
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
"crypto/rand"
|
"crypto/rand"
|
||||||
"encoding/binary"
|
"fmt"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
func uint32rand() (value uint32) {
|
|
||||||
err := binary.Read(rand.Reader, binary.LittleEndian, &value)
|
|
||||||
if err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// Generates a time ordered UUID. Top 32 bits are a timestamp,
|
// Generates a time ordered UUID. Top 32 bits are a timestamp,
|
||||||
// bottom 96 are random.
|
// bottom 96 are random.
|
||||||
func TimeOrderedUUID() string {
|
func TimeOrderedUUID() string {
|
||||||
unix := uint32(time.Now().UTC().Unix())
|
unix := uint32(time.Now().UTC().Unix())
|
||||||
rand1 := uint32rand()
|
|
||||||
rand2 := uint32rand()
|
b := make([]byte, 12)
|
||||||
rand3 := uint32rand()
|
n, err := rand.Read(b)
|
||||||
|
if n != len(b) {
|
||||||
|
err = fmt.Errorf("Not enough entropy available")
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
return fmt.Sprintf("%08x-%04x-%04x-%04x-%04x%08x",
|
return fmt.Sprintf("%08x-%04x-%04x-%04x-%04x%08x",
|
||||||
unix,
|
unix, b[0:2], b[2:4], b[4:6], b[6:8], b[8:])
|
||||||
uint16(rand1>>16),
|
|
||||||
uint16(rand1&0xffff),
|
|
||||||
uint16(rand2>>16),
|
|
||||||
uint16(rand2&0xffff),
|
|
||||||
rand3)
|
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in new issue