From 9f5ebd0a941c688b3428e93d5b09443f42bd478b Mon Sep 17 00:00:00 2001 From: Stephen J Day Date: Wed, 6 Jan 2016 17:24:58 -0800 Subject: [PATCH] digest: add NewDigestFromBytes for completeness Signed-off-by: Stephen J Day --- digest/digest.go | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/digest/digest.go b/digest/digest.go index 75f16e7f..31d821bb 100644 --- a/digest/digest.go +++ b/digest/digest.go @@ -28,7 +28,15 @@ type Digest string // NewDigest returns a Digest from alg and a hash.Hash object. func NewDigest(alg Algorithm, h hash.Hash) Digest { - return Digest(fmt.Sprintf("%s:%x", alg, h.Sum(nil))) + return NewDigestFromBytes(alg, h.Sum(nil)) +} + +// NewDigestFromBytes returns a new digest from the byte contents of p. +// Typically, this can come from hash.Hash.Sum(...) or xxx.SumXXX(...) +// functions. This is also useful for rebuilding digests from binary +// serializations. +func NewDigestFromBytes(alg Algorithm, p []byte) Digest { + return Digest(fmt.Sprintf("%s:%x", alg, p)) } // NewDigestFromHex returns a Digest from alg and a the hex encoded digest.