Merge pull request #1307 from stevvooe/panic-on-unavailable

digest: panic on unavailable hash algorithm
This commit is contained in:
Stephen Day 2015-12-29 18:47:02 -08:00
commit 0404e5a622

View file

@ -2,6 +2,7 @@ package digest
import ( import (
"crypto" "crypto"
"fmt"
"hash" "hash"
"io" "io"
) )
@ -84,11 +85,18 @@ func (a Algorithm) New() Digester {
} }
} }
// Hash returns a new hash as used by the algorithm. If not available, nil is // Hash returns a new hash as used by the algorithm. If not available, the
// returned. Make sure to check Available before calling. // method will panic. Check Algorithm.Available() before calling.
func (a Algorithm) Hash() hash.Hash { func (a Algorithm) Hash() hash.Hash {
if !a.Available() { if !a.Available() {
return nil // NOTE(stevvooe): A missing hash is usually a programming error that
// must be resolved at compile time. We don't import in the digest
// package to allow users to choose their hash implementation (such as
// when using stevvooe/resumable or a hardware accelerated package).
//
// Applications that may want to resolve the hash at runtime should
// call Algorithm.Available before call Algorithm.Hash().
panic(fmt.Sprintf("%v not available (make sure it is imported)", a))
} }
return algorithms[a].New() return algorithms[a].New()