Skip to content

Commit

Permalink
digest: make FromBytes available on digest.Algorithm
Browse files Browse the repository at this point in the history
Signed-off-by: Stephen J Day <stephen.day@docker.com>
  • Loading branch information
stevvooe committed Dec 29, 2015
1 parent 5c5ed3c commit 41d9591
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 12 deletions.
13 changes: 1 addition & 12 deletions digest.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,18 +69,7 @@ func FromReader(rd io.Reader) (Digest, error) {

// FromBytes digests the input and returns a Digest.
func FromBytes(p []byte) Digest {
digester := Canonical.New()

if _, err := digester.Hash().Write(p); err != nil {
// Writes to a Hash should never fail. None of the existing
// hash implementations in the stdlib or hashes vendored
// here can return errors from Write. Having a panic in this
// condition instead of having FromBytes return an error value
// avoids unnecessary error handling paths in all callers.
panic("write to hash function returned error: " + err.Error())
}

return digester.Digest()
return Canonical.FromBytes(p)
}

// Validate checks that the contents of d is a valid digest, returning an
Expand Down
16 changes: 16 additions & 0 deletions digester.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,22 @@ func (a Algorithm) FromReader(rd io.Reader) (Digest, error) {
return digester.Digest(), nil
}

// FromBytes digests the input and returns a Digest.
func (a Algorithm) FromBytes(p []byte) Digest {
digester := a.New()

if _, err := digester.Hash().Write(p); err != nil {
// Writes to a Hash should never fail. None of the existing
// hash implementations in the stdlib or hashes vendored
// here can return errors from Write. Having a panic in this
// condition instead of having FromBytes return an error value
// avoids unnecessary error handling paths in all callers.
panic("write to hash function returned error: " + err.Error())
}

return digester.Digest()
}

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

Expand Down

0 comments on commit 41d9591

Please sign in to comment.