digest.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. // Copyright 2019, 2020 OCI Contributors
  2. // Copyright 2017 Docker, Inc.
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. //
  8. // https://www.apache.org/licenses/LICENSE-2.0
  9. //
  10. // Unless required by applicable law or agreed to in writing, software
  11. // distributed under the License is distributed on an "AS IS" BASIS,
  12. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. // See the License for the specific language governing permissions and
  14. // limitations under the License.
  15. package digest
  16. import (
  17. "fmt"
  18. "hash"
  19. "io"
  20. "regexp"
  21. "strings"
  22. )
  23. // Digest allows simple protection of hex formatted digest strings, prefixed
  24. // by their algorithm. Strings of type Digest have some guarantee of being in
  25. // the correct format and it provides quick access to the components of a
  26. // digest string.
  27. //
  28. // The following is an example of the contents of Digest types:
  29. //
  30. // sha256:7173b809ca12ec5dee4506cd86be934c4596dd234ee82c0662eac04a8c2c71dc
  31. //
  32. // This allows to abstract the digest behind this type and work only in those
  33. // terms.
  34. type Digest string
  35. // NewDigest returns a Digest from alg and a hash.Hash object.
  36. func NewDigest(alg Algorithm, h hash.Hash) Digest {
  37. return NewDigestFromBytes(alg, h.Sum(nil))
  38. }
  39. // NewDigestFromBytes returns a new digest from the byte contents of p.
  40. // Typically, this can come from hash.Hash.Sum(...) or xxx.SumXXX(...)
  41. // functions. This is also useful for rebuilding digests from binary
  42. // serializations.
  43. func NewDigestFromBytes(alg Algorithm, p []byte) Digest {
  44. return NewDigestFromEncoded(alg, alg.Encode(p))
  45. }
  46. // NewDigestFromHex is deprecated. Please use NewDigestFromEncoded.
  47. func NewDigestFromHex(alg, hex string) Digest {
  48. return NewDigestFromEncoded(Algorithm(alg), hex)
  49. }
  50. // NewDigestFromEncoded returns a Digest from alg and the encoded digest.
  51. func NewDigestFromEncoded(alg Algorithm, encoded string) Digest {
  52. return Digest(fmt.Sprintf("%s:%s", alg, encoded))
  53. }
  54. // DigestRegexp matches valid digest types.
  55. var DigestRegexp = regexp.MustCompile(`[a-z0-9]+(?:[.+_-][a-z0-9]+)*:[a-zA-Z0-9=_-]+`)
  56. // DigestRegexpAnchored matches valid digest types, anchored to the start and end of the match.
  57. var DigestRegexpAnchored = regexp.MustCompile(`^` + DigestRegexp.String() + `$`)
  58. var (
  59. // ErrDigestInvalidFormat returned when digest format invalid.
  60. ErrDigestInvalidFormat = fmt.Errorf("invalid checksum digest format")
  61. // ErrDigestInvalidLength returned when digest has invalid length.
  62. ErrDigestInvalidLength = fmt.Errorf("invalid checksum digest length")
  63. // ErrDigestUnsupported returned when the digest algorithm is unsupported.
  64. ErrDigestUnsupported = fmt.Errorf("unsupported digest algorithm")
  65. )
  66. // Parse parses s and returns the validated digest object. An error will
  67. // be returned if the format is invalid.
  68. func Parse(s string) (Digest, error) {
  69. d := Digest(s)
  70. return d, d.Validate()
  71. }
  72. // FromReader consumes the content of rd until io.EOF, returning canonical digest.
  73. func FromReader(rd io.Reader) (Digest, error) {
  74. return Canonical.FromReader(rd)
  75. }
  76. // FromBytes digests the input and returns a Digest.
  77. func FromBytes(p []byte) Digest {
  78. return Canonical.FromBytes(p)
  79. }
  80. // FromString digests the input and returns a Digest.
  81. func FromString(s string) Digest {
  82. return Canonical.FromString(s)
  83. }
  84. // Validate checks that the contents of d is a valid digest, returning an
  85. // error if not.
  86. func (d Digest) Validate() error {
  87. s := string(d)
  88. i := strings.Index(s, ":")
  89. if i <= 0 || i+1 == len(s) {
  90. return ErrDigestInvalidFormat
  91. }
  92. algorithm, encoded := Algorithm(s[:i]), s[i+1:]
  93. if !algorithm.Available() {
  94. if !DigestRegexpAnchored.MatchString(s) {
  95. return ErrDigestInvalidFormat
  96. }
  97. return ErrDigestUnsupported
  98. }
  99. return algorithm.Validate(encoded)
  100. }
  101. // Algorithm returns the algorithm portion of the digest. This will panic if
  102. // the underlying digest is not in a valid format.
  103. func (d Digest) Algorithm() Algorithm {
  104. return Algorithm(d[:d.sepIndex()])
  105. }
  106. // Verifier returns a writer object that can be used to verify a stream of
  107. // content against the digest. If the digest is invalid, the method will panic.
  108. func (d Digest) Verifier() Verifier {
  109. return hashVerifier{
  110. hash: d.Algorithm().Hash(),
  111. digest: d,
  112. }
  113. }
  114. // Encoded returns the encoded portion of the digest. This will panic if the
  115. // underlying digest is not in a valid format.
  116. func (d Digest) Encoded() string {
  117. return string(d[d.sepIndex()+1:])
  118. }
  119. // Hex is deprecated. Please use Digest.Encoded.
  120. func (d Digest) Hex() string {
  121. return d.Encoded()
  122. }
  123. func (d Digest) String() string {
  124. return string(d)
  125. }
  126. func (d Digest) sepIndex() int {
  127. i := strings.Index(string(d), ":")
  128. if i < 0 {
  129. panic(fmt.Sprintf("no ':' separator in digest %q", d))
  130. }
  131. return i
  132. }