algorithm.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. // Copyright 2017 Docker, Inc.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // https://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package digest
  15. import (
  16. "crypto"
  17. "fmt"
  18. "hash"
  19. "io"
  20. "regexp"
  21. )
  22. // Algorithm identifies and implementation of a digester by an identifier.
  23. // Note the that this defines both the hash algorithm used and the string
  24. // encoding.
  25. type Algorithm string
  26. // supported digest types
  27. const (
  28. SHA256 Algorithm = "sha256" // sha256 with hex encoding (lower case only)
  29. SHA384 Algorithm = "sha384" // sha384 with hex encoding (lower case only)
  30. SHA512 Algorithm = "sha512" // sha512 with hex encoding (lower case only)
  31. // Canonical is the primary digest algorithm used with the distribution
  32. // project. Other digests may be used but this one is the primary storage
  33. // digest.
  34. Canonical = SHA256
  35. )
  36. var (
  37. // TODO(stevvooe): Follow the pattern of the standard crypto package for
  38. // registration of digests. Effectively, we are a registerable set and
  39. // common symbol access.
  40. // algorithms maps values to hash.Hash implementations. Other algorithms
  41. // may be available but they cannot be calculated by the digest package.
  42. algorithms = map[Algorithm]crypto.Hash{
  43. SHA256: crypto.SHA256,
  44. SHA384: crypto.SHA384,
  45. SHA512: crypto.SHA512,
  46. }
  47. // anchoredEncodedRegexps contains anchored regular expressions for hex-encoded digests.
  48. // Note that /A-F/ disallowed.
  49. anchoredEncodedRegexps = map[Algorithm]*regexp.Regexp{
  50. SHA256: regexp.MustCompile(`^[a-f0-9]{64}$`),
  51. SHA384: regexp.MustCompile(`^[a-f0-9]{96}$`),
  52. SHA512: regexp.MustCompile(`^[a-f0-9]{128}$`),
  53. }
  54. )
  55. // Available returns true if the digest type is available for use. If this
  56. // returns false, Digester and Hash will return nil.
  57. func (a Algorithm) Available() bool {
  58. h, ok := algorithms[a]
  59. if !ok {
  60. return false
  61. }
  62. // check availability of the hash, as well
  63. return h.Available()
  64. }
  65. func (a Algorithm) String() string {
  66. return string(a)
  67. }
  68. // Size returns number of bytes returned by the hash.
  69. func (a Algorithm) Size() int {
  70. h, ok := algorithms[a]
  71. if !ok {
  72. return 0
  73. }
  74. return h.Size()
  75. }
  76. // Set implemented to allow use of Algorithm as a command line flag.
  77. func (a *Algorithm) Set(value string) error {
  78. if value == "" {
  79. *a = Canonical
  80. } else {
  81. // just do a type conversion, support is queried with Available.
  82. *a = Algorithm(value)
  83. }
  84. if !a.Available() {
  85. return ErrDigestUnsupported
  86. }
  87. return nil
  88. }
  89. // Digester returns a new digester for the specified algorithm. If the algorithm
  90. // does not have a digester implementation, nil will be returned. This can be
  91. // checked by calling Available before calling Digester.
  92. func (a Algorithm) Digester() Digester {
  93. return &digester{
  94. alg: a,
  95. hash: a.Hash(),
  96. }
  97. }
  98. // Hash returns a new hash as used by the algorithm. If not available, the
  99. // method will panic. Check Algorithm.Available() before calling.
  100. func (a Algorithm) Hash() hash.Hash {
  101. if !a.Available() {
  102. // Empty algorithm string is invalid
  103. if a == "" {
  104. panic(fmt.Sprintf("empty digest algorithm, validate before calling Algorithm.Hash()"))
  105. }
  106. // NOTE(stevvooe): A missing hash is usually a programming error that
  107. // must be resolved at compile time. We don't import in the digest
  108. // package to allow users to choose their hash implementation (such as
  109. // when using stevvooe/resumable or a hardware accelerated package).
  110. //
  111. // Applications that may want to resolve the hash at runtime should
  112. // call Algorithm.Available before call Algorithm.Hash().
  113. panic(fmt.Sprintf("%v not available (make sure it is imported)", a))
  114. }
  115. return algorithms[a].New()
  116. }
  117. // Encode encodes the raw bytes of a digest, typically from a hash.Hash, into
  118. // the encoded portion of the digest.
  119. func (a Algorithm) Encode(d []byte) string {
  120. // TODO(stevvooe): Currently, all algorithms use a hex encoding. When we
  121. // add support for back registration, we can modify this accordingly.
  122. return fmt.Sprintf("%x", d)
  123. }
  124. // FromReader returns the digest of the reader using the algorithm.
  125. func (a Algorithm) FromReader(rd io.Reader) (Digest, error) {
  126. digester := a.Digester()
  127. if _, err := io.Copy(digester.Hash(), rd); err != nil {
  128. return "", err
  129. }
  130. return digester.Digest(), nil
  131. }
  132. // FromBytes digests the input and returns a Digest.
  133. func (a Algorithm) FromBytes(p []byte) Digest {
  134. digester := a.Digester()
  135. if _, err := digester.Hash().Write(p); err != nil {
  136. // Writes to a Hash should never fail. None of the existing
  137. // hash implementations in the stdlib or hashes vendored
  138. // here can return errors from Write. Having a panic in this
  139. // condition instead of having FromBytes return an error value
  140. // avoids unnecessary error handling paths in all callers.
  141. panic("write to hash function returned error: " + err.Error())
  142. }
  143. return digester.Digest()
  144. }
  145. // FromString digests the string input and returns a Digest.
  146. func (a Algorithm) FromString(s string) Digest {
  147. return a.FromBytes([]byte(s))
  148. }
  149. // Validate validates the encoded portion string
  150. func (a Algorithm) Validate(encoded string) error {
  151. r, ok := anchoredEncodedRegexps[a]
  152. if !ok {
  153. return ErrDigestUnsupported
  154. }
  155. // Digests much always be hex-encoded, ensuring that their hex portion will
  156. // always be size*2
  157. if a.Size()*2 != len(encoded) {
  158. return ErrDigestInvalidLength
  159. }
  160. if r.MatchString(encoded) {
  161. return nil
  162. }
  163. return ErrDigestInvalidFormat
  164. }