1
0
mirror of https://github.com/drone/drone-cli.git synced 2025-08-22 13:44:30 +02:00
drone-cli/common/uuid/uuid.go
2015-02-27 00:15:49 -08:00

29 lines
550 B
Go

package uuid
import (
"crypto/rand"
"crypto/sha1"
"fmt"
"io"
)
// CreateUUID is a helper function that will
// create a random, unique identifier.
func CreateUUID() string {
c := sha1.New()
r := CreateRandom()
io.WriteString(c, string(r))
s := fmt.Sprintf("%x", c.Sum(nil))
return s[0:10]
}
// CreateRandom creates a random block of bytes
// that we can use to generate unique identifiers.
func CreateRandom() []byte {
k := make([]byte, sha1.BlockSize)
if _, err := io.ReadFull(rand.Reader, k); err != nil {
return nil
}
return k
}