54512f2184
Had these laying around; not very critical as these functions are barely used, but probably won't hurt: BenchmarkIsShortID-10 1447230 816.9 ns/op 0 B/op 0 allocs/op BenchmarkIsShortIDNew-10 10103319 101.9 ns/op 0 B/op 0 allocs/op BenchmarkValidateID-10 10000 112208 ns/op 33005 B/op 10 allocs/op BenchmarkValidateIDNew-10 653293 1737 ns/op 16534 B/op 6 allocs/op Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
73 lines
1.9 KiB
Go
73 lines
1.9 KiB
Go
// Package stringid provides helper functions for dealing with string identifiers
|
|
package stringid // import "github.com/docker/docker/pkg/stringid"
|
|
|
|
import (
|
|
"crypto/rand"
|
|
"encoding/hex"
|
|
"errors"
|
|
"regexp"
|
|
"strconv"
|
|
"strings"
|
|
)
|
|
|
|
const (
|
|
shortLen = 12
|
|
fullLen = 64
|
|
)
|
|
|
|
var (
|
|
validShortID = regexp.MustCompile("^[a-f0-9]{12}$")
|
|
validHex = regexp.MustCompile(`^[a-f0-9]{64}$`)
|
|
)
|
|
|
|
// IsShortID determines if id has the correct format and length for a short ID.
|
|
// It checks the IDs length and if it consists of valid characters for IDs (a-f0-9).
|
|
func IsShortID(id string) bool {
|
|
if len(id) != shortLen {
|
|
return false
|
|
}
|
|
return validShortID.MatchString(id)
|
|
}
|
|
|
|
// 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 longer prefix, or the full-length Id.
|
|
func TruncateID(id string) string {
|
|
if i := strings.IndexRune(id, ':'); i >= 0 {
|
|
id = id[i+1:]
|
|
}
|
|
if len(id) > shortLen {
|
|
id = id[:shortLen]
|
|
}
|
|
return id
|
|
}
|
|
|
|
// GenerateRandomID returns a unique id.
|
|
func GenerateRandomID() string {
|
|
b := make([]byte, 32)
|
|
for {
|
|
if _, err := rand.Read(b); err != nil {
|
|
panic(err) // This shouldn't happen
|
|
}
|
|
id := hex.EncodeToString(b)
|
|
// if we try to parse the truncated for as an int and we don't have
|
|
// an error then the value is all numeric and causes issues when
|
|
// used as a hostname. ref #3869
|
|
if _, err := strconv.ParseInt(TruncateID(id), 10, 64); err == nil {
|
|
continue
|
|
}
|
|
return id
|
|
}
|
|
}
|
|
|
|
// ValidateID checks whether an ID string is a valid, full-length image ID.
|
|
func ValidateID(id string) error {
|
|
if len(id) != fullLen {
|
|
return errors.New("image ID '" + id + "' is invalid")
|
|
}
|
|
if !validHex.MatchString(id) {
|
|
return errors.New("image ID '" + id + "' is invalid")
|
|
}
|
|
return nil
|
|
}
|