summaryrefslogtreecommitdiff
path: root/vendor/github.com/opencontainers/go-digest/algorithm.go
diff options
context:
space:
mode:
Diffstat (limited to 'vendor/github.com/opencontainers/go-digest/algorithm.go')
-rw-r--r--vendor/github.com/opencontainers/go-digest/algorithm.go40
1 files changed, 37 insertions, 3 deletions
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
+}