2015-07-25 08:35:07 +00:00
|
|
|
// Package stringid provides helper functions for dealing with string identifiers
|
2015-03-24 11:25:26 +00:00
|
|
|
package stringid
|
2015-02-21 04:48:23 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/rand"
|
|
|
|
"encoding/hex"
|
|
|
|
"io"
|
2015-04-10 17:09:08 +00:00
|
|
|
"regexp"
|
2015-02-21 04:48:23 +00:00
|
|
|
"strconv"
|
2015-11-18 22:20:54 +00:00
|
|
|
"strings"
|
2015-07-29 00:16:06 +00:00
|
|
|
|
|
|
|
"github.com/docker/docker/pkg/random"
|
2015-02-21 04:48:23 +00:00
|
|
|
)
|
|
|
|
|
2015-04-10 17:09:08 +00:00
|
|
|
const shortLen = 12
|
|
|
|
|
2015-05-13 18:48:48 +00:00
|
|
|
var validShortID = regexp.MustCompile("^[a-z0-9]{12}$")
|
|
|
|
|
2015-07-25 08:35:07 +00:00
|
|
|
// IsShortID determines if an arbitrary string *looks like* a short ID.
|
2015-04-10 17:09:08 +00:00
|
|
|
func IsShortID(id string) bool {
|
2015-05-13 18:48:48 +00:00
|
|
|
return validShortID.MatchString(id)
|
2015-04-10 17:09:08 +00:00
|
|
|
}
|
|
|
|
|
2015-02-21 04:48:23 +00:00
|
|
|
// TruncateID returns a shorthand version of a string identifier for convenience.
|
|
|
|
// A collision with other shorthands is very unlikely, but possible.
|
|
|
|
// In case of a collision a lookup with TruncIndex.Get() will fail, and the caller
|
|
|
|
// will need to use a langer prefix, or the full-length Id.
|
|
|
|
func TruncateID(id string) string {
|
2015-11-18 22:20:54 +00:00
|
|
|
if i := strings.IndexRune(id, ':'); i >= 0 {
|
|
|
|
id = id[i+1:]
|
|
|
|
}
|
2015-04-10 17:09:08 +00:00
|
|
|
trimTo := shortLen
|
2015-02-21 04:48:23 +00:00
|
|
|
if len(id) < shortLen {
|
2015-04-10 17:09:08 +00:00
|
|
|
trimTo = len(id)
|
2015-02-21 04:48:23 +00:00
|
|
|
}
|
2015-04-10 17:09:08 +00:00
|
|
|
return id[:trimTo]
|
2015-02-21 04:48:23 +00:00
|
|
|
}
|
|
|
|
|
2015-07-29 00:16:06 +00:00
|
|
|
func generateID(crypto bool) string {
|
|
|
|
b := make([]byte, 32)
|
2015-11-02 16:02:25 +00:00
|
|
|
r := random.Reader
|
2015-07-29 00:16:06 +00:00
|
|
|
if crypto {
|
|
|
|
r = rand.Reader
|
|
|
|
}
|
2015-02-21 04:48:23 +00:00
|
|
|
for {
|
2015-07-29 00:16:06 +00:00
|
|
|
if _, err := io.ReadFull(r, b); err != nil {
|
2015-02-21 04:48:23 +00:00
|
|
|
panic(err) // This shouldn't happen
|
|
|
|
}
|
2015-07-29 00:16:06 +00:00
|
|
|
id := hex.EncodeToString(b)
|
2015-02-21 04:48:23 +00:00
|
|
|
// if we try to parse the truncated for as an int and we don't have
|
|
|
|
// an error then the value is all numberic and causes issues when
|
|
|
|
// used as a hostname. ref #3869
|
2015-07-29 00:16:06 +00:00
|
|
|
if _, err := strconv.ParseInt(TruncateID(id), 10, 64); err == nil {
|
2015-02-21 04:48:23 +00:00
|
|
|
continue
|
|
|
|
}
|
2015-07-29 00:16:06 +00:00
|
|
|
return id
|
2015-02-21 04:48:23 +00:00
|
|
|
}
|
|
|
|
}
|
2015-07-29 00:16:06 +00:00
|
|
|
|
|
|
|
// GenerateRandomID returns an unique id.
|
|
|
|
func GenerateRandomID() string {
|
|
|
|
return generateID(true)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// GenerateNonCryptoID generates unique id without using cryptographically
|
|
|
|
// secure sources of random.
|
|
|
|
// It helps you to save entropy.
|
|
|
|
func GenerateNonCryptoID() string {
|
|
|
|
return generateID(false)
|
|
|
|
}
|