Skip to content

Commit

Permalink
Fixes missing entropy in the uuid package.
Browse files Browse the repository at this point in the history
math/crypto is seeded with 1 and thus will create predictable UUIDs. Because
amazon-instance and amazon-ebs in the same second when building both targets
the timestamp in front doesn't help either. See #552
  • Loading branch information
zimbatm committed Oct 23, 2013
1 parent 5b1463f commit fa0a0a8
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions common/uuid/uuid.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,26 @@ package uuid

import (
"fmt"
"math/rand"
"crypto/rand"
"encoding/binary"
"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,
// bottom 96 are random.
func TimeOrderedUUID() string {
unix := uint32(time.Now().UTC().Unix())
rand1 := rand.Uint32()
rand2 := rand.Uint32()
rand3 := rand.Uint32()
rand1 := uint32rand()
rand2 := uint32rand()
rand3 := uint32rand()
return fmt.Sprintf("%08x-%04x-%04x-%04x-%04x%08x",
unix,
uint16(rand1>>16),
Expand Down

0 comments on commit fa0a0a8

Please sign in to comment.