digester.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. package digest
  2. import (
  3. "crypto"
  4. "fmt"
  5. "hash"
  6. "io"
  7. )
  8. // Algorithm identifies and implementation of a digester by an identifier.
  9. // Note the that this defines both the hash algorithm used and the string
  10. // encoding.
  11. type Algorithm string
  12. // supported digest types
  13. const (
  14. SHA256 Algorithm = "sha256" // sha256 with hex encoding
  15. SHA384 Algorithm = "sha384" // sha384 with hex encoding
  16. SHA512 Algorithm = "sha512" // sha512 with hex encoding
  17. // Canonical is the primary digest algorithm used with the distribution
  18. // project. Other digests may be used but this one is the primary storage
  19. // digest.
  20. Canonical = SHA256
  21. )
  22. var (
  23. // TODO(stevvooe): Follow the pattern of the standard crypto package for
  24. // registration of digests. Effectively, we are a registerable set and
  25. // common symbol access.
  26. // algorithms maps values to hash.Hash implementations. Other algorithms
  27. // may be available but they cannot be calculated by the digest package.
  28. algorithms = map[Algorithm]crypto.Hash{
  29. SHA256: crypto.SHA256,
  30. SHA384: crypto.SHA384,
  31. SHA512: crypto.SHA512,
  32. }
  33. )
  34. // Available returns true if the digest type is available for use. If this
  35. // returns false, New and Hash will return nil.
  36. func (a Algorithm) Available() bool {
  37. h, ok := algorithms[a]
  38. if !ok {
  39. return false
  40. }
  41. // check availability of the hash, as well
  42. return h.Available()
  43. }
  44. func (a Algorithm) String() string {
  45. return string(a)
  46. }
  47. // Size returns number of bytes returned by the hash.
  48. func (a Algorithm) Size() int {
  49. h, ok := algorithms[a]
  50. if !ok {
  51. return 0
  52. }
  53. return h.Size()
  54. }
  55. // Set implemented to allow use of Algorithm as a command line flag.
  56. func (a *Algorithm) Set(value string) error {
  57. if value == "" {
  58. *a = Canonical
  59. } else {
  60. // just do a type conversion, support is queried with Available.
  61. *a = Algorithm(value)
  62. }
  63. return nil
  64. }
  65. // New returns a new digester for the specified algorithm. If the algorithm
  66. // does not have a digester implementation, nil will be returned. This can be
  67. // checked by calling Available before calling New.
  68. func (a Algorithm) New() Digester {
  69. return &digester{
  70. alg: a,
  71. hash: a.Hash(),
  72. }
  73. }
  74. // Hash returns a new hash as used by the algorithm. If not available, the
  75. // method will panic. Check Algorithm.Available() before calling.
  76. func (a Algorithm) Hash() hash.Hash {
  77. if !a.Available() {
  78. // NOTE(stevvooe): A missing hash is usually a programming error that
  79. // must be resolved at compile time. We don't import in the digest
  80. // package to allow users to choose their hash implementation (such as
  81. // when using stevvooe/resumable or a hardware accelerated package).
  82. //
  83. // Applications that may want to resolve the hash at runtime should
  84. // call Algorithm.Available before call Algorithm.Hash().
  85. panic(fmt.Sprintf("%v not available (make sure it is imported)", a))
  86. }
  87. return algorithms[a].New()
  88. }
  89. // FromReader returns the digest of the reader using the algorithm.
  90. func (a Algorithm) FromReader(rd io.Reader) (Digest, error) {
  91. digester := a.New()
  92. if _, err := io.Copy(digester.Hash(), rd); err != nil {
  93. return "", err
  94. }
  95. return digester.Digest(), nil
  96. }
  97. // FromBytes digests the input and returns a Digest.
  98. func (a Algorithm) FromBytes(p []byte) Digest {
  99. digester := a.New()
  100. if _, err := digester.Hash().Write(p); err != nil {
  101. // Writes to a Hash should never fail. None of the existing
  102. // hash implementations in the stdlib or hashes vendored
  103. // here can return errors from Write. Having a panic in this
  104. // condition instead of having FromBytes return an error value
  105. // avoids unnecessary error handling paths in all callers.
  106. panic("write to hash function returned error: " + err.Error())
  107. }
  108. return digester.Digest()
  109. }
  110. // TODO(stevvooe): Allow resolution of verifiers using the digest type and
  111. // this registration system.
  112. // Digester calculates the digest of written data. Writes should go directly
  113. // to the return value of Hash, while calling Digest will return the current
  114. // value of the digest.
  115. type Digester interface {
  116. Hash() hash.Hash // provides direct access to underlying hash instance.
  117. Digest() Digest
  118. }
  119. // digester provides a simple digester definition that embeds a hasher.
  120. type digester struct {
  121. alg Algorithm
  122. hash hash.Hash
  123. }
  124. func (d *digester) Hash() hash.Hash {
  125. return d.hash
  126. }
  127. func (d *digester) Digest() Digest {
  128. return NewDigest(d.alg, d.hash)
  129. }