digest.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. package digest
  2. import (
  3. "fmt"
  4. "hash"
  5. "io"
  6. "regexp"
  7. "strings"
  8. )
  9. const (
  10. // DigestSha256EmptyTar is the canonical sha256 digest of empty data
  11. DigestSha256EmptyTar = "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"
  12. )
  13. // Digest allows simple protection of hex formatted digest strings, prefixed
  14. // by their algorithm. Strings of type Digest have some guarantee of being in
  15. // the correct format and it provides quick access to the components of a
  16. // digest string.
  17. //
  18. // The following is an example of the contents of Digest types:
  19. //
  20. // sha256:7173b809ca12ec5dee4506cd86be934c4596dd234ee82c0662eac04a8c2c71dc
  21. //
  22. // This allows to abstract the digest behind this type and work only in those
  23. // terms.
  24. type Digest string
  25. // NewDigest returns a Digest from alg and a hash.Hash object.
  26. func NewDigest(alg Algorithm, h hash.Hash) Digest {
  27. return NewDigestFromBytes(alg, h.Sum(nil))
  28. }
  29. // NewDigestFromBytes returns a new digest from the byte contents of p.
  30. // Typically, this can come from hash.Hash.Sum(...) or xxx.SumXXX(...)
  31. // functions. This is also useful for rebuilding digests from binary
  32. // serializations.
  33. func NewDigestFromBytes(alg Algorithm, p []byte) Digest {
  34. return Digest(fmt.Sprintf("%s:%x", alg, p))
  35. }
  36. // NewDigestFromHex returns a Digest from alg and a the hex encoded digest.
  37. func NewDigestFromHex(alg, hex string) Digest {
  38. return Digest(fmt.Sprintf("%s:%s", alg, hex))
  39. }
  40. // DigestRegexp matches valid digest types.
  41. var DigestRegexp = regexp.MustCompile(`[a-zA-Z0-9-_+.]+:[a-fA-F0-9]+`)
  42. // DigestRegexpAnchored matches valid digest types, anchored to the start and end of the match.
  43. var DigestRegexpAnchored = regexp.MustCompile(`^` + DigestRegexp.String() + `$`)
  44. var (
  45. // ErrDigestInvalidFormat returned when digest format invalid.
  46. ErrDigestInvalidFormat = fmt.Errorf("invalid checksum digest format")
  47. // ErrDigestInvalidLength returned when digest has invalid length.
  48. ErrDigestInvalidLength = fmt.Errorf("invalid checksum digest length")
  49. // ErrDigestUnsupported returned when the digest algorithm is unsupported.
  50. ErrDigestUnsupported = fmt.Errorf("unsupported digest algorithm")
  51. )
  52. // ParseDigest parses s and returns the validated digest object. An error will
  53. // be returned if the format is invalid.
  54. func ParseDigest(s string) (Digest, error) {
  55. d := Digest(s)
  56. return d, d.Validate()
  57. }
  58. // FromReader returns the most valid digest for the underlying content using
  59. // the canonical digest algorithm.
  60. func FromReader(rd io.Reader) (Digest, error) {
  61. return Canonical.FromReader(rd)
  62. }
  63. // FromBytes digests the input and returns a Digest.
  64. func FromBytes(p []byte) Digest {
  65. return Canonical.FromBytes(p)
  66. }
  67. // Validate checks that the contents of d is a valid digest, returning an
  68. // error if not.
  69. func (d Digest) Validate() error {
  70. s := string(d)
  71. if !DigestRegexpAnchored.MatchString(s) {
  72. return ErrDigestInvalidFormat
  73. }
  74. i := strings.Index(s, ":")
  75. if i < 0 {
  76. return ErrDigestInvalidFormat
  77. }
  78. // case: "sha256:" with no hex.
  79. if i+1 == len(s) {
  80. return ErrDigestInvalidFormat
  81. }
  82. switch algorithm := Algorithm(s[:i]); algorithm {
  83. case SHA256, SHA384, SHA512:
  84. if algorithm.Size()*2 != len(s[i+1:]) {
  85. return ErrDigestInvalidLength
  86. }
  87. break
  88. default:
  89. return ErrDigestUnsupported
  90. }
  91. return nil
  92. }
  93. // Algorithm returns the algorithm portion of the digest. This will panic if
  94. // the underlying digest is not in a valid format.
  95. func (d Digest) Algorithm() Algorithm {
  96. return Algorithm(d[:d.sepIndex()])
  97. }
  98. // Hex returns the hex digest portion of the digest. This will panic if the
  99. // underlying digest is not in a valid format.
  100. func (d Digest) Hex() string {
  101. return string(d[d.sepIndex()+1:])
  102. }
  103. func (d Digest) String() string {
  104. return string(d)
  105. }
  106. func (d Digest) sepIndex() int {
  107. i := strings.Index(string(d), ":")
  108. if i < 0 {
  109. panic("could not find ':' in digest: " + d)
  110. }
  111. return i
  112. }