Forráskód Böngészése

Simplify id generation

Instead of using a SHA-256 of a random number, hex encode 32 bytes
of random data from crypto/rand (sourced from /dev/urandom).
Jonathan Rudenberg 12 éve
szülő
commit
141b5fc7d7
1 módosított fájl, 7 hozzáadás és 16 törlés
  1. 7 16
      image.go

+ 7 - 16
image.go

@@ -1,13 +1,12 @@
 package docker
 package docker
 
 
 import (
 import (
-	"bytes"
-	"crypto/sha256"
+	"crypto/rand"
+	"encoding/hex"
 	"encoding/json"
 	"encoding/json"
 	"fmt"
 	"fmt"
 	"io"
 	"io"
 	"io/ioutil"
 	"io/ioutil"
-	"math/rand"
 	"os"
 	"os"
 	"path"
 	"path"
 	"strings"
 	"strings"
@@ -162,20 +161,12 @@ func ValidateId(id string) error {
 }
 }
 
 
 func GenerateId() string {
 func GenerateId() string {
-	// FIXME: don't seed every time
-	rand.Seed(time.Now().UTC().UnixNano())
-	randomBytes := bytes.NewBuffer([]byte(fmt.Sprintf("%x", rand.Int())))
-	id, _ := ComputeId(randomBytes) // can't fail
-	return id
-}
-
-// ComputeId reads from `content` until EOF, then returns a SHA of what it read, as a string.
-func ComputeId(content io.Reader) (string, error) {
-	h := sha256.New()
-	if _, err := io.Copy(h, content); err != nil {
-		return "", err
+	id := make([]byte, 32)
+	_, err := io.ReadFull(rand.Reader, id)
+	if err != nil {
+		panic(err) // This shouldn't happen
 	}
 	}
-	return fmt.Sprintf("%x", h.Sum(nil)), nil
+	return hex.EncodeToString(id)
 }
 }
 
 
 // Image includes convenience proxy functions to its graph
 // Image includes convenience proxy functions to its graph