2014-11-19 22:23:01 +01:00
|
|
|
package digest
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"crypto/sha256"
|
|
|
|
"fmt"
|
|
|
|
"hash"
|
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
2014-12-19 03:21:57 +01:00
|
|
|
"regexp"
|
2014-11-19 22:23:01 +01:00
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/docker/docker/pkg/tarsum"
|
|
|
|
)
|
|
|
|
|
2015-02-02 22:01:49 +01:00
|
|
|
const (
|
|
|
|
// DigestTarSumV1EmptyTar is the digest for the empty tar file.
|
|
|
|
DigestTarSumV1EmptyTar = "tarsum.v1+sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"
|
2015-03-05 05:26:56 +01:00
|
|
|
// DigestSha256EmptyTar is the canonical sha256 digest of empty data
|
|
|
|
DigestSha256EmptyTar = "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"
|
2015-02-02 22:01:49 +01:00
|
|
|
)
|
|
|
|
|
2014-11-19 22:23:01 +01: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
|
|
|
|
//
|
|
|
|
// More important for this code base, this type is compatible with tarsum
|
|
|
|
// digests. For example, the following would be a valid Digest:
|
|
|
|
//
|
|
|
|
// tarsum+sha256:e58fcf7418d4390dec8e8fb69d88c06ec07039d651fedd3aa72af9972e7d046b
|
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
func NewDigest(alg string, h hash.Hash) Digest {
|
|
|
|
return Digest(fmt.Sprintf("%s:%x", alg, h.Sum(nil)))
|
|
|
|
}
|
|
|
|
|
2015-01-14 06:35:42 +01: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 03:21:57 +01:00
|
|
|
// DigestRegexp matches valid digest types.
|
2015-03-10 22:40:58 +01:00
|
|
|
var DigestRegexp = regexp.MustCompile(`[a-zA-Z0-9-_+.]+:[a-fA-F0-9]+`)
|
2014-12-19 03:21:57 +01:00
|
|
|
|
2015-03-11 04:02:30 +01:00
|
|
|
// DigestRegexpAnchored matches valid digest types, anchored to the start and end of the match.
|
|
|
|
var DigestRegexpAnchored = regexp.MustCompile(`^` + DigestRegexp.String() + `$`)
|
|
|
|
|
2014-11-19 22:23:01 +01:00
|
|
|
var (
|
|
|
|
// ErrDigestInvalidFormat returned when digest format invalid.
|
|
|
|
ErrDigestInvalidFormat = fmt.Errorf("invalid checksum digest format")
|
|
|
|
|
2015-03-19 02:26:09 +01:00
|
|
|
// ErrDigestUnsupported returned when the digest algorithm is unsupported.
|
2014-11-19 22:23:01 +01:00
|
|
|
ErrDigestUnsupported = fmt.Errorf("unsupported digest algorithm")
|
|
|
|
)
|
|
|
|
|
|
|
|
// ParseDigest parses s and returns the validated digest object. An error will
|
|
|
|
// be returned if the format is invalid.
|
|
|
|
func ParseDigest(s string) (Digest, error) {
|
2014-11-25 01:21:02 +01:00
|
|
|
d := Digest(s)
|
2014-11-19 22:23:01 +01:00
|
|
|
|
2014-11-25 01:21:02 +01:00
|
|
|
return d, d.Validate()
|
2014-11-19 22:23:01 +01:00
|
|
|
}
|
|
|
|
|
2014-11-19 23:59:05 +01:00
|
|
|
// FromReader returns the most valid digest for the underlying content.
|
|
|
|
func FromReader(rd io.Reader) (Digest, error) {
|
2015-01-14 06:35:42 +01:00
|
|
|
h := sha256.New()
|
2014-11-19 22:23:01 +01:00
|
|
|
|
2015-01-14 06:35:42 +01:00
|
|
|
if _, err := io.Copy(h, rd); err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2014-11-19 22:23:01 +01:00
|
|
|
|
2015-01-14 06:35:42 +01:00
|
|
|
return NewDigest("sha256", h), nil
|
|
|
|
}
|
2014-11-19 22:23:01 +01:00
|
|
|
|
2015-01-14 06:35:42 +01:00
|
|
|
// FromTarArchive produces a tarsum digest from reader rd.
|
|
|
|
func FromTarArchive(rd io.Reader) (Digest, error) {
|
|
|
|
ts, err := tarsum.NewTarSum(rd, true, tarsum.Version1)
|
2014-11-19 22:23:01 +01:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := io.Copy(ioutil.Discard, ts); err != nil {
|
2015-01-14 06:35:42 +01:00
|
|
|
return "", err
|
2014-11-19 22:23:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
d, err := ParseDigest(ts.Sum(nil))
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return d, nil
|
|
|
|
}
|
|
|
|
|
2014-11-19 23:59:05 +01:00
|
|
|
// FromBytes digests the input and returns a Digest.
|
|
|
|
func FromBytes(p []byte) (Digest, error) {
|
|
|
|
return FromReader(bytes.NewReader(p))
|
2014-11-19 22:23:01 +01:00
|
|
|
}
|
|
|
|
|
2014-11-25 01:21:02 +01: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)
|
|
|
|
// Common case will be tarsum
|
2015-01-06 01:04:30 +01:00
|
|
|
_, err := ParseTarSum(s)
|
2014-11-25 01:21:02 +01:00
|
|
|
if err == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Continue on for general parser
|
|
|
|
|
2015-03-11 04:02:30 +01:00
|
|
|
if !DigestRegexpAnchored.MatchString(s) {
|
2015-03-04 09:02:50 +01:00
|
|
|
return ErrDigestInvalidFormat
|
|
|
|
}
|
|
|
|
|
2014-11-25 01:21:02 +01:00
|
|
|
i := strings.Index(s, ":")
|
|
|
|
if i < 0 {
|
|
|
|
return ErrDigestInvalidFormat
|
|
|
|
}
|
|
|
|
|
|
|
|
// case: "sha256:" with no hex.
|
|
|
|
if i+1 == len(s) {
|
|
|
|
return ErrDigestInvalidFormat
|
|
|
|
}
|
|
|
|
|
|
|
|
switch s[:i] {
|
2015-02-20 02:55:05 +01:00
|
|
|
case "sha256", "sha384", "sha512":
|
2014-11-25 01:21:02 +01:00
|
|
|
break
|
|
|
|
default:
|
|
|
|
return ErrDigestUnsupported
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-11-19 22:23:01 +01:00
|
|
|
// Algorithm returns the algorithm portion of the digest. This will panic if
|
|
|
|
// the underlying digest is not in a valid format.
|
|
|
|
func (d Digest) Algorithm() string {
|
|
|
|
return string(d[:d.sepIndex()])
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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 {
|
2015-03-10 22:40:58 +01:00
|
|
|
panic("could not find ':' in digest: " + d)
|
2014-11-19 22:23:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return i
|
|
|
|
}
|