From 4646282448d60f138dd17f016e46e797c7f032d9 Mon Sep 17 00:00:00 2001 From: Stephen J Day Date: Tue, 29 Dec 2015 15:16:56 -0800 Subject: [PATCH] digest: make FromBytes available on digest.Algorithm Signed-off-by: Stephen J Day --- digest/digest.go | 13 +------------ digest/digester.go | 16 ++++++++++++++++ 2 files changed, 17 insertions(+), 12 deletions(-) diff --git a/digest/digest.go b/digest/digest.go index f3e12bca4..75f16e7fc 100644 --- a/digest/digest.go +++ b/digest/digest.go @@ -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 diff --git a/digest/digester.go b/digest/digester.go index cbb2e3683..98a3d634c 100644 --- a/digest/digester.go +++ b/digest/digester.go @@ -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.