Merge pull request #557 from zimbatm/unique-uuids

Fixes missing entropy in the uuid package.
This commit is contained in:
Matthew Hooker 2013-10-23 15:23:46 -07:00
commit 2b14a9b625
1 changed files with 11 additions and 10 deletions

View File

@ -1,8 +1,8 @@
package uuid package uuid
import ( import (
"crypto/rand"
"fmt" "fmt"
"math/rand"
"time" "time"
) )
@ -10,14 +10,15 @@ import (
// 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 := rand.Uint32()
rand2 := rand.Uint32() b := make([]byte, 12)
rand3 := rand.Uint32() n, err := rand.Read(b)
return fmt.Sprintf("%08x-%04x-%04x-%04x-%04x%08x", if n != len(b) {
unix, err = fmt.Errorf("Not enough entropy available")
uint16(rand1>>16), }
uint16(rand1&0xffff), if err != nil {
uint16(rand2>>16), panic(err)
uint16(rand2&0xffff), }
rand3) return fmt.Sprintf("%08x-%04x-%04x-%04x-%04x%08x",
unix, b[0:2], b[2:4], b[4:6], b[6:8], b[8:])
} }