Skip to content

Commit

Permalink
Add generic content digest tool
Browse files Browse the repository at this point in the history
Previously a useful gist, this changeset polishes the original tarsum tool into
a utility that can be used to calculate content digests. Any algorithm from the
digest package is supported with additional support from tarsum.

This tool is very useful for quickly checking backend digests and verifying
correctness.

Signed-off-by: Stephen J Day <[email protected]>
  • Loading branch information
stevvooe committed Aug 20, 2015
1 parent 2d4d92c commit 446200d
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 8 deletions.
11 changes: 3 additions & 8 deletions digest.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,15 +70,10 @@ func ParseDigest(s string) (Digest, error) {
return d, d.Validate()
}

// FromReader returns the most valid digest for the underlying content.
// FromReader returns the most valid digest for the underlying content using
// the canonical digest algorithm.
func FromReader(rd io.Reader) (Digest, error) {
digester := Canonical.New()

if _, err := io.Copy(digester.Hash(), rd); err != nil {
return "", err
}

return digester.Digest(), nil
return Canonical.FromReader(rd)
}

// FromTarArchive produces a tarsum digest from reader rd.
Expand Down
28 changes: 28 additions & 0 deletions digester.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package digest
import (
"crypto"
"hash"
"io"
)

// Algorithm identifies and implementation of a digester by an identifier.
Expand Down Expand Up @@ -49,6 +50,22 @@ func (a Algorithm) Available() bool {
return h.Available()
}

func (a Algorithm) String() string {
return string(a)
}

// Set implemented to allow use of Algorithm as a command line flag.
func (a *Algorithm) Set(value string) error {
if value == "" {
*a = Canonical
} else {
// just do a type conversion, support is queried with Available.
*a = Algorithm(value)
}

return nil
}

// New returns a new digester for the specified algorithm. If the algorithm
// does not have a digester implementation, nil will be returned. This can be
// checked by calling Available before calling New.
Expand All @@ -69,6 +86,17 @@ func (a Algorithm) Hash() hash.Hash {
return algorithms[a].New()
}

// FromReader returns the digest of the reader using the algorithm.
func (a Algorithm) FromReader(rd io.Reader) (Digest, error) {
digester := a.New()

if _, err := io.Copy(digester.Hash(), rd); err != nil {
return "", err
}

return digester.Digest(), nil
}

// TODO(stevvooe): Allow resolution of verifiers using the digest type and
// this registration system.

Expand Down

0 comments on commit 446200d

Please sign in to comment.