2015-07-25 04:35:07 -04:00
|
|
|
// Package stringid provides helper functions for dealing with string identifiers
|
2018-02-05 16:05:59 -05:00
|
|
|
package stringid // import "github.com/docker/docker/pkg/stringid"
|
2015-02-20 23:48:23 -05:00
|
|
|
|
|
|
|
import (
|
2017-05-08 20:02:02 -04:00
|
|
|
cryptorand "crypto/rand"
|
2015-02-20 23:48:23 -05:00
|
|
|
"encoding/hex"
|
2016-12-22 16:26:30 -05:00
|
|
|
"fmt"
|
2015-02-20 23:48:23 -05:00
|
|
|
"io"
|
2017-05-08 20:02:02 -04:00
|
|
|
"math"
|
|
|
|
"math/big"
|
|
|
|
"math/rand"
|
2015-04-10 13:09:08 -04:00
|
|
|
"regexp"
|
2015-02-20 23:48:23 -05:00
|
|
|
"strconv"
|
2015-11-18 17:20:54 -05:00
|
|
|
"strings"
|
2017-05-08 20:02:02 -04:00
|
|
|
"time"
|
2015-02-20 23:48:23 -05:00
|
|
|
)
|
|
|
|
|
2015-04-10 13:09:08 -04:00
|
|
|
const shortLen = 12
|
|
|
|
|
2016-12-22 16:26:30 -05:00
|
|
|
var (
|
|
|
|
validShortID = regexp.MustCompile("^[a-f0-9]{12}$")
|
|
|
|
validHex = regexp.MustCompile(`^[a-f0-9]{64}$`)
|
|
|
|
)
|
2015-05-13 14:48:48 -04:00
|
|
|
|
2015-07-25 04:35:07 -04:00
|
|
|
// IsShortID determines if an arbitrary string *looks like* a short ID.
|
2015-04-10 13:09:08 -04:00
|
|
|
func IsShortID(id string) bool {
|
2015-05-13 14:48:48 -04:00
|
|
|
return validShortID.MatchString(id)
|
2015-04-10 13:09:08 -04:00
|
|
|
}
|
|
|
|
|
2015-02-20 23:48:23 -05: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
|
2016-04-09 09:18:15 -04:00
|
|
|
// will need to use a longer prefix, or the full-length Id.
|
2015-02-20 23:48:23 -05:00
|
|
|
func TruncateID(id string) string {
|
2015-11-18 17:20:54 -05:00
|
|
|
if i := strings.IndexRune(id, ':'); i >= 0 {
|
|
|
|
id = id[i+1:]
|
|
|
|
}
|
2016-07-19 18:20:12 -04:00
|
|
|
if len(id) > shortLen {
|
|
|
|
id = id[:shortLen]
|
2015-02-20 23:48:23 -05:00
|
|
|
}
|
2016-07-19 18:20:12 -04:00
|
|
|
return id
|
2015-02-20 23:48:23 -05:00
|
|
|
}
|
|
|
|
|
2017-05-08 20:02:02 -04:00
|
|
|
func generateID(r io.Reader) string {
|
2015-07-28 20:16:06 -04:00
|
|
|
b := make([]byte, 32)
|
2015-02-20 23:48:23 -05:00
|
|
|
for {
|
2015-07-28 20:16:06 -04:00
|
|
|
if _, err := io.ReadFull(r, b); err != nil {
|
2015-02-20 23:48:23 -05:00
|
|
|
panic(err) // This shouldn't happen
|
|
|
|
}
|
2015-07-28 20:16:06 -04:00
|
|
|
id := hex.EncodeToString(b)
|
2015-02-20 23:48:23 -05:00
|
|
|
// if we try to parse the truncated for as an int and we don't have
|
2015-12-13 11:00:39 -05:00
|
|
|
// an error then the value is all numeric and causes issues when
|
2015-02-20 23:48:23 -05:00
|
|
|
// used as a hostname. ref #3869
|
2015-07-28 20:16:06 -04:00
|
|
|
if _, err := strconv.ParseInt(TruncateID(id), 10, 64); err == nil {
|
2015-02-20 23:48:23 -05:00
|
|
|
continue
|
|
|
|
}
|
2015-07-28 20:16:06 -04:00
|
|
|
return id
|
2015-02-20 23:48:23 -05:00
|
|
|
}
|
|
|
|
}
|
2015-07-28 20:16:06 -04:00
|
|
|
|
2016-04-09 09:18:15 -04:00
|
|
|
// GenerateRandomID returns a unique id.
|
2015-07-28 20:16:06 -04:00
|
|
|
func GenerateRandomID() string {
|
2017-05-08 20:02:02 -04:00
|
|
|
return generateID(cryptorand.Reader)
|
2015-07-28 20:16:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// GenerateNonCryptoID generates unique id without using cryptographically
|
|
|
|
// secure sources of random.
|
|
|
|
// It helps you to save entropy.
|
|
|
|
func GenerateNonCryptoID() string {
|
2017-05-08 20:02:02 -04:00
|
|
|
return generateID(readerFunc(rand.Read))
|
2015-07-28 20:16:06 -04:00
|
|
|
}
|
2016-12-22 16:26:30 -05:00
|
|
|
|
|
|
|
// ValidateID checks whether an ID string is a valid image ID.
|
|
|
|
func ValidateID(id string) error {
|
|
|
|
if ok := validHex.MatchString(id); !ok {
|
|
|
|
return fmt.Errorf("image ID %q is invalid", id)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2017-05-08 20:02:02 -04:00
|
|
|
|
|
|
|
func init() {
|
|
|
|
// safely set the seed globally so we generate random ids. Tries to use a
|
|
|
|
// crypto seed before falling back to time.
|
|
|
|
var seed int64
|
|
|
|
if cryptoseed, err := cryptorand.Int(cryptorand.Reader, big.NewInt(math.MaxInt64)); err != nil {
|
|
|
|
// This should not happen, but worst-case fallback to time-based seed.
|
|
|
|
seed = time.Now().UnixNano()
|
|
|
|
} else {
|
|
|
|
seed = cryptoseed.Int64()
|
|
|
|
}
|
|
|
|
|
|
|
|
rand.Seed(seed)
|
|
|
|
}
|
|
|
|
|
|
|
|
type readerFunc func(p []byte) (int, error)
|
|
|
|
|
|
|
|
func (fn readerFunc) Read(p []byte) (int, error) {
|
|
|
|
return fn(p)
|
|
|
|
}
|