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).
This commit is contained in:
Jonathan Rudenberg 2013-03-26 16:46:27 -04:00
parent 2ee3db6cd2
commit 141b5fc7d7
1 changed files with 7 additions and 16 deletions

View File

@ -1,13 +1,12 @@
package docker
import (
"bytes"
"crypto/sha256"
"crypto/rand"
"encoding/hex"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"math/rand"
"os"
"path"
"strings"
@ -162,20 +161,12 @@ func ValidateId(id string) error {
}
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