2014-11-19 21:23:01 +00:00
|
|
|
package digest
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"hash"
|
|
|
|
"io"
|
2014-12-19 02:21:57 +00:00
|
|
|
"regexp"
|
2014-11-19 21:23:01 +00:00
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2015-02-02 21:01:49 +00:00
|
|
|
const (
|
2015-03-05 04:26:56 +00:00
|
|
|
// DigestSha256EmptyTar is the canonical sha256 digest of empty data
|
|
|
|
DigestSha256EmptyTar = "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"
|
2015-02-02 21:01:49 +00:00
|
|
|
)
|
|
|
|
|
2014-11-19 21:23:01 +00:00
|
|
|
// Digest allows simple protection of hex formatted digest strings, prefixed
|
|
|
|
// by their algorithm. Strings of type Digest have some guarantee of being in
|
|
|
|
// the correct format and it provides quick access to the components of a
|
|
|
|
// digest string.
|
|
|
|
//
|
|
|
|
// The following is an example of the contents of Digest types:
|
|
|
|
//
|
|
|
|
// sha256:7173b809ca12ec5dee4506cd86be934c4596dd234ee82c0662eac04a8c2c71dc
|
|
|
|
//
|
|
|
|
// This allows to abstract the digest behind this type and work only in those
|
|
|
|
// terms.
|
|
|
|
type Digest string
|
|
|
|
|
|
|
|
// NewDigest returns a Digest from alg and a hash.Hash object.
|
2015-05-22 01:44:08 +00:00
|
|
|
func NewDigest(alg Algorithm, h hash.Hash) Digest {
|
2016-01-07 01:24:58 +00:00
|
|
|
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))
|
2014-11-19 21:23:01 +00:00
|
|
|
}
|
|
|
|
|
2015-01-14 05:35:42 +00:00
|
|
|
// NewDigestFromHex returns a Digest from alg and a the hex encoded digest.
|
|
|
|
func NewDigestFromHex(alg, hex string) Digest {
|
|
|
|
return Digest(fmt.Sprintf("%s:%s", alg, hex))
|
|
|
|
}
|
|
|
|
|
2014-12-19 02:21:57 +00:00
|
|
|
// DigestRegexp matches valid digest types.
|
2015-03-10 21:40:58 +00:00
|
|
|
var DigestRegexp = regexp.MustCompile(`[a-zA-Z0-9-_+.]+:[a-fA-F0-9]+`)
|
2014-12-19 02:21:57 +00:00
|
|
|
|
2015-03-11 03:02:30 +00:00
|
|
|
// DigestRegexpAnchored matches valid digest types, anchored to the start and end of the match.
|
|
|
|
var DigestRegexpAnchored = regexp.MustCompile(`^` + DigestRegexp.String() + `$`)
|
|
|
|
|
2014-11-19 21:23:01 +00:00
|
|
|
var (
|
|
|
|
// ErrDigestInvalidFormat returned when digest format invalid.
|
|
|
|
ErrDigestInvalidFormat = fmt.Errorf("invalid checksum digest format")
|
|
|
|
|
2015-12-02 23:57:47 +00:00
|
|
|
// ErrDigestInvalidLength returned when digest has invalid length.
|
|
|
|
ErrDigestInvalidLength = fmt.Errorf("invalid checksum digest length")
|
|
|
|
|
2015-03-19 01:26:09 +00:00
|
|
|
// ErrDigestUnsupported returned when the digest algorithm is unsupported.
|
2014-11-19 21:23:01 +00:00
|
|
|
ErrDigestUnsupported = fmt.Errorf("unsupported digest algorithm")
|
|
|
|
)
|
|
|
|
|
2016-12-16 06:38:38 +00:00
|
|
|
// Parse parses s and returns the validated digest object. An error will
|
2014-11-19 21:23:01 +00:00
|
|
|
// be returned if the format is invalid.
|
2016-12-15 22:58:54 +00:00
|
|
|
func Parse(s string) (Digest, error) {
|
2014-11-25 00:21:02 +00:00
|
|
|
d := Digest(s)
|
|
|
|
return d, d.Validate()
|
2014-11-19 21:23:01 +00:00
|
|
|
}
|
|
|
|
|
2016-12-15 22:58:54 +00:00
|
|
|
// ParseDigest is deprecated. Use Parse.
|
|
|
|
func ParseDigest(s string) (Digest, error) {
|
|
|
|
return Parse(s)
|
|
|
|
}
|
|
|
|
|
2015-08-20 19:39:50 +00:00
|
|
|
// FromReader returns the most valid digest for the underlying content using
|
|
|
|
// the canonical digest algorithm.
|
2014-11-19 22:59:05 +00:00
|
|
|
func FromReader(rd io.Reader) (Digest, error) {
|
2015-08-20 19:39:50 +00:00
|
|
|
return Canonical.FromReader(rd)
|
2015-01-14 05:35:42 +00:00
|
|
|
}
|
2014-11-19 21:23:01 +00:00
|
|
|
|
2014-11-19 22:59:05 +00:00
|
|
|
// FromBytes digests the input and returns a Digest.
|
2015-12-14 22:30:51 +00:00
|
|
|
func FromBytes(p []byte) Digest {
|
2015-12-29 23:16:56 +00:00
|
|
|
return Canonical.FromBytes(p)
|
2014-11-19 21:23:01 +00:00
|
|
|
}
|
|
|
|
|
2016-12-07 21:44:37 +00:00
|
|
|
// FromString digests the input and returns a Digest.
|
|
|
|
func FromString(s string) Digest {
|
|
|
|
return Canonical.FromString(s)
|
|
|
|
}
|
|
|
|
|
2014-11-25 00:21:02 +00:00
|
|
|
// Validate checks that the contents of d is a valid digest, returning an
|
|
|
|
// error if not.
|
|
|
|
func (d Digest) Validate() error {
|
|
|
|
s := string(d)
|
|
|
|
|
|
|
|
i := strings.Index(s, ":")
|
|
|
|
|
2016-12-16 01:21:57 +00:00
|
|
|
// validate i then run through regexp
|
|
|
|
if i < 0 || i+1 == len(s) || !DigestRegexpAnchored.MatchString(s) {
|
2014-11-25 00:21:02 +00:00
|
|
|
return ErrDigestInvalidFormat
|
|
|
|
}
|
|
|
|
|
2016-12-16 00:30:53 +00:00
|
|
|
algorithm := Algorithm(s[:i])
|
|
|
|
if !algorithm.Available() {
|
2014-11-25 00:21:02 +00:00
|
|
|
return ErrDigestUnsupported
|
|
|
|
}
|
|
|
|
|
2016-12-16 00:30:53 +00:00
|
|
|
// Digests much always be hex-encoded, ensuring that their hex portion will
|
|
|
|
// always be size*2
|
|
|
|
if algorithm.Size()*2 != len(s[i+1:]) {
|
|
|
|
return ErrDigestInvalidLength
|
|
|
|
}
|
|
|
|
|
2014-11-25 00:21:02 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-11-19 21:23:01 +00:00
|
|
|
// Algorithm returns the algorithm portion of the digest. This will panic if
|
|
|
|
// the underlying digest is not in a valid format.
|
2015-05-22 01:44:08 +00:00
|
|
|
func (d Digest) Algorithm() Algorithm {
|
|
|
|
return Algorithm(d[:d.sepIndex()])
|
2014-11-19 21:23:01 +00:00
|
|
|
}
|
|
|
|
|
2016-12-15 23:06:18 +00:00
|
|
|
// Verifier returns a writer object that can be used to verify a stream of
|
2016-12-16 00:30:53 +00:00
|
|
|
// content against the digest. If the digest is invalid, the method will panic.
|
|
|
|
func (d Digest) Verifier() Verifier {
|
2016-12-15 23:06:18 +00:00
|
|
|
return hashVerifier{
|
|
|
|
hash: d.Algorithm().Hash(),
|
|
|
|
digest: d,
|
2016-12-16 00:30:53 +00:00
|
|
|
}
|
2016-12-15 23:06:18 +00:00
|
|
|
}
|
|
|
|
|
2014-11-19 21:23:01 +00:00
|
|
|
// Hex returns the hex digest portion of the digest. This will panic if the
|
|
|
|
// underlying digest is not in a valid format.
|
|
|
|
func (d Digest) Hex() string {
|
|
|
|
return string(d[d.sepIndex()+1:])
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d Digest) String() string {
|
|
|
|
return string(d)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d Digest) sepIndex() int {
|
|
|
|
i := strings.Index(string(d), ":")
|
|
|
|
|
|
|
|
if i < 0 {
|
2016-12-16 00:30:53 +00:00
|
|
|
panic(fmt.Sprintf("no ':' separator in digest %q", d))
|
2014-11-19 21:23:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return i
|
|
|
|
}
|