stringid.go 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. // Package stringid provides helper functions for dealing with string identifiers
  2. package stringid
  3. import (
  4. "crypto/rand"
  5. "encoding/hex"
  6. "io"
  7. "regexp"
  8. "strconv"
  9. "strings"
  10. "github.com/docker/docker/pkg/random"
  11. )
  12. const shortLen = 12
  13. var validShortID = regexp.MustCompile("^[a-z0-9]{12}$")
  14. // IsShortID determines if an arbitrary string *looks like* a short ID.
  15. func IsShortID(id string) bool {
  16. return validShortID.MatchString(id)
  17. }
  18. // TruncateID returns a shorthand version of a string identifier for convenience.
  19. // A collision with other shorthands is very unlikely, but possible.
  20. // In case of a collision a lookup with TruncIndex.Get() will fail, and the caller
  21. // will need to use a longer prefix, or the full-length Id.
  22. func TruncateID(id string) string {
  23. if i := strings.IndexRune(id, ':'); i >= 0 {
  24. id = id[i+1:]
  25. }
  26. if len(id) > shortLen {
  27. id = id[:shortLen]
  28. }
  29. return id
  30. }
  31. func generateID(crypto bool) string {
  32. b := make([]byte, 32)
  33. r := random.Reader
  34. if crypto {
  35. r = rand.Reader
  36. }
  37. for {
  38. if _, err := io.ReadFull(r, b); err != nil {
  39. panic(err) // This shouldn't happen
  40. }
  41. id := hex.EncodeToString(b)
  42. // if we try to parse the truncated for as an int and we don't have
  43. // an error then the value is all numeric and causes issues when
  44. // used as a hostname. ref #3869
  45. if _, err := strconv.ParseInt(TruncateID(id), 10, 64); err == nil {
  46. continue
  47. }
  48. return id
  49. }
  50. }
  51. // GenerateRandomID returns a unique id.
  52. func GenerateRandomID() string {
  53. return generateID(true)
  54. }
  55. // GenerateNonCryptoID generates unique id without using cryptographically
  56. // secure sources of random.
  57. // It helps you to save entropy.
  58. func GenerateNonCryptoID() string {
  59. return generateID(false)
  60. }