summaryrefslogtreecommitdiff
path: root/vendor/github.com/opencontainers/go-digest
diff options
context:
space:
mode:
Diffstat (limited to 'vendor/github.com/opencontainers/go-digest')
-rw-r--r--vendor/github.com/opencontainers/go-digest/LICENSE (renamed from vendor/github.com/opencontainers/go-digest/LICENSE.code)0
-rw-r--r--vendor/github.com/opencontainers/go-digest/README.md2
-rw-r--r--vendor/github.com/opencontainers/go-digest/algorithm.go40
-rw-r--r--vendor/github.com/opencontainers/go-digest/digest.go42
4 files changed, 60 insertions, 24 deletions
diff --git a/vendor/github.com/opencontainers/go-digest/LICENSE.code b/vendor/github.com/opencontainers/go-digest/LICENSE
index 0ea3ff81e..0ea3ff81e 100644
--- a/vendor/github.com/opencontainers/go-digest/LICENSE.code
+++ b/vendor/github.com/opencontainers/go-digest/LICENSE
diff --git a/vendor/github.com/opencontainers/go-digest/README.md b/vendor/github.com/opencontainers/go-digest/README.md
index 0f5a04092..25aac3470 100644
--- a/vendor/github.com/opencontainers/go-digest/README.md
+++ b/vendor/github.com/opencontainers/go-digest/README.md
@@ -101,4 +101,4 @@ the various OCI projects).
# Copyright and license
-Copyright © 2016 Docker, Inc. All rights reserved, except as follows. Code is released under the [Apache 2.0 license](LICENSE.code). This `README.md` file and the [`CONTRIBUTING.md`](CONTRIBUTING.md) file are licensed under the Creative Commons Attribution 4.0 International License under the terms and conditions set forth in the file [`LICENSE.docs`](LICENSE.docs). You may obtain a duplicate copy of the same license, titled CC BY-SA 4.0, at http://creativecommons.org/licenses/by-sa/4.0/.
+Copyright © 2016 Docker, Inc. All rights reserved, except as follows. Code is released under the [Apache 2.0 license](LICENSE). This `README.md` file and the [`CONTRIBUTING.md`](CONTRIBUTING.md) file are licensed under the Creative Commons Attribution 4.0 International License under the terms and conditions set forth in the file [`LICENSE.docs`](LICENSE.docs). You may obtain a duplicate copy of the same license, titled CC BY-SA 4.0, at http://creativecommons.org/licenses/by-sa/4.0/.
diff --git a/vendor/github.com/opencontainers/go-digest/algorithm.go b/vendor/github.com/opencontainers/go-digest/algorithm.go
index bdff42d92..8813bd26f 100644
--- a/vendor/github.com/opencontainers/go-digest/algorithm.go
+++ b/vendor/github.com/opencontainers/go-digest/algorithm.go
@@ -19,6 +19,7 @@ import (
"fmt"
"hash"
"io"
+ "regexp"
)
// Algorithm identifies and implementation of a digester by an identifier.
@@ -28,9 +29,9 @@ type Algorithm string
// supported digest types
const (
- SHA256 Algorithm = "sha256" // sha256 with hex encoding
- SHA384 Algorithm = "sha384" // sha384 with hex encoding
- SHA512 Algorithm = "sha512" // sha512 with hex encoding
+ SHA256 Algorithm = "sha256" // sha256 with hex encoding (lower case only)
+ SHA384 Algorithm = "sha384" // sha384 with hex encoding (lower case only)
+ SHA512 Algorithm = "sha512" // sha512 with hex encoding (lower case only)
// Canonical is the primary digest algorithm used with the distribution
// project. Other digests may be used but this one is the primary storage
@@ -50,6 +51,14 @@ var (
SHA384: crypto.SHA384,
SHA512: crypto.SHA512,
}
+
+ // anchoredEncodedRegexps contains anchored regular expressions for hex-encoded digests.
+ // Note that /A-F/ disallowed.
+ anchoredEncodedRegexps = map[Algorithm]*regexp.Regexp{
+ SHA256: regexp.MustCompile(`^[a-f0-9]{64}$`),
+ SHA384: regexp.MustCompile(`^[a-f0-9]{96}$`),
+ SHA512: regexp.MustCompile(`^[a-f0-9]{128}$`),
+ }
)
// Available returns true if the digest type is available for use. If this
@@ -125,6 +134,14 @@ func (a Algorithm) Hash() hash.Hash {
return algorithms[a].New()
}
+// Encode encodes the raw bytes of a digest, typically from a hash.Hash, into
+// the encoded portion of the digest.
+func (a Algorithm) Encode(d []byte) string {
+ // TODO(stevvooe): Currently, all algorithms use a hex encoding. When we
+ // add support for back registration, we can modify this accordingly.
+ return fmt.Sprintf("%x", d)
+}
+
// FromReader returns the digest of the reader using the algorithm.
func (a Algorithm) FromReader(rd io.Reader) (Digest, error) {
digester := a.Digester()
@@ -156,3 +173,20 @@ func (a Algorithm) FromBytes(p []byte) Digest {
func (a Algorithm) FromString(s string) Digest {
return a.FromBytes([]byte(s))
}
+
+// Validate validates the encoded portion string
+func (a Algorithm) Validate(encoded string) error {
+ r, ok := anchoredEncodedRegexps[a]
+ if !ok {
+ return ErrDigestUnsupported
+ }
+ // Digests much always be hex-encoded, ensuring that their hex portion will
+ // always be size*2
+ if a.Size()*2 != len(encoded) {
+ return ErrDigestInvalidLength
+ }
+ if r.MatchString(encoded) {
+ return nil
+ }
+ return ErrDigestInvalidFormat
+}
diff --git a/vendor/github.com/opencontainers/go-digest/digest.go b/vendor/github.com/opencontainers/go-digest/digest.go
index 69e1d2b54..ad398cba2 100644
--- a/vendor/github.com/opencontainers/go-digest/digest.go
+++ b/vendor/github.com/opencontainers/go-digest/digest.go
@@ -45,16 +45,21 @@ func NewDigest(alg Algorithm, h hash.Hash) Digest {
// 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))
+ return NewDigestFromEncoded(alg, alg.Encode(p))
}
-// NewDigestFromHex returns a Digest from alg and a the hex encoded digest.
+// NewDigestFromHex is deprecated. Please use NewDigestFromEncoded.
func NewDigestFromHex(alg, hex string) Digest {
- return Digest(fmt.Sprintf("%s:%s", alg, hex))
+ return NewDigestFromEncoded(Algorithm(alg), hex)
+}
+
+// NewDigestFromEncoded returns a Digest from alg and the encoded digest.
+func NewDigestFromEncoded(alg Algorithm, encoded string) Digest {
+ return Digest(fmt.Sprintf("%s:%s", alg, encoded))
}
// DigestRegexp matches valid digest types.
-var DigestRegexp = regexp.MustCompile(`[a-zA-Z0-9-_+.]+:[a-fA-F0-9]+`)
+var DigestRegexp = regexp.MustCompile(`[a-z0-9]+(?:[.+_-][a-z0-9]+)*:[a-zA-Z0-9=_-]+`)
// DigestRegexpAnchored matches valid digest types, anchored to the start and end of the match.
var DigestRegexpAnchored = regexp.MustCompile(`^` + DigestRegexp.String() + `$`)
@@ -96,26 +101,18 @@ func FromString(s string) Digest {
// error if not.
func (d Digest) Validate() error {
s := string(d)
-
i := strings.Index(s, ":")
-
- // validate i then run through regexp
- if i < 0 || i+1 == len(s) || !DigestRegexpAnchored.MatchString(s) {
+ if i <= 0 || i+1 == len(s) {
return ErrDigestInvalidFormat
}
-
- algorithm := Algorithm(s[:i])
+ algorithm, encoded := Algorithm(s[:i]), s[i+1:]
if !algorithm.Available() {
+ if !DigestRegexpAnchored.MatchString(s) {
+ return ErrDigestInvalidFormat
+ }
return ErrDigestUnsupported
}
-
- // 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
- }
-
- return nil
+ return algorithm.Validate(encoded)
}
// Algorithm returns the algorithm portion of the digest. This will panic if
@@ -133,12 +130,17 @@ func (d Digest) Verifier() Verifier {
}
}
-// Hex returns the hex digest portion of the digest. This will panic if the
+// Encoded returns the encoded portion of the digest. This will panic if the
// underlying digest is not in a valid format.
-func (d Digest) Hex() string {
+func (d Digest) Encoded() string {
return string(d[d.sepIndex()+1:])
}
+// Hex is deprecated. Please use Digest.Encoded.
+func (d Digest) Hex() string {
+ return d.Encoded()
+}
+
func (d Digest) String() string {
return string(d)
}