algorithm.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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, Digester 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. if !a.Available() {
  64. return ErrDigestUnsupported
  65. }
  66. return nil
  67. }
  68. // Digester returns a new digester for the specified algorithm. If the algorithm
  69. // does not have a digester implementation, nil will be returned. This can be
  70. // checked by calling Available before calling Digester.
  71. func (a Algorithm) Digester() Digester {
  72. return &digester{
  73. alg: a,
  74. hash: a.Hash(),
  75. }
  76. }
  77. // Hash returns a new hash as used by the algorithm. If not available, the
  78. // method will panic. Check Algorithm.Available() before calling.
  79. func (a Algorithm) Hash() hash.Hash {
  80. if !a.Available() {
  81. // Empty algorithm string is invalid
  82. if a == "" {
  83. panic(fmt.Sprintf("empty digest algorithm, validate before calling Algorithm.Hash()"))
  84. }
  85. // NOTE(stevvooe): A missing hash is usually a programming error that
  86. // must be resolved at compile time. We don't import in the digest
  87. // package to allow users to choose their hash implementation (such as
  88. // when using stevvooe/resumable or a hardware accelerated package).
  89. //
  90. // Applications that may want to resolve the hash at runtime should
  91. // call Algorithm.Available before call Algorithm.Hash().
  92. panic(fmt.Sprintf("%v not available (make sure it is imported)", a))
  93. }
  94. return algorithms[a].New()
  95. }
  96. // FromReader returns the digest of the reader using the algorithm.
  97. func (a Algorithm) FromReader(rd io.Reader) (Digest, error) {
  98. digester := a.Digester()
  99. if _, err := io.Copy(digester.Hash(), rd); err != nil {
  100. return "", err
  101. }
  102. return digester.Digest(), nil
  103. }
  104. // FromBytes digests the input and returns a Digest.
  105. func (a Algorithm) FromBytes(p []byte) Digest {
  106. digester := a.Digester()
  107. if _, err := digester.Hash().Write(p); err != nil {
  108. // Writes to a Hash should never fail. None of the existing
  109. // hash implementations in the stdlib or hashes vendored
  110. // here can return errors from Write. Having a panic in this
  111. // condition instead of having FromBytes return an error value
  112. // avoids unnecessary error handling paths in all callers.
  113. panic("write to hash function returned error: " + err.Error())
  114. }
  115. return digester.Digest()
  116. }
  117. // FromString digests the string input and returns a Digest.
  118. func (a Algorithm) FromString(s string) Digest {
  119. return a.FromBytes([]byte(s))
  120. }