Skip to content

Commit

Permalink
Refactor specification of supported digests
Browse files Browse the repository at this point in the history
To make the definition of supported digests more clear, we have refactored the
digest package to have a special Algorithm type. This represents the digest's
prefix and we associated various supported hash implementations through
function calls.

Signed-off-by: Stephen J Day <stephen.day@docker.com>
  • Loading branch information
stevvooe authored and dmcgowan committed May 10, 2020
1 parent 77570c9 commit 96bf78c
Showing 1 changed file with 7 additions and 7 deletions.
14 changes: 7 additions & 7 deletions digestset/set.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,17 +42,17 @@ func NewSet() *Set {
// values or short values. This function does not test equality,
// rather whether the second value could match against the first
// value.
func checkShortMatch(alg, hex, shortAlg, shortHex string) bool {
func checkShortMatch(alg Algorithm, hex, shortAlg, shortHex string) bool {
if len(hex) == len(shortHex) {
if hex != shortHex {
return false
}
if len(shortAlg) > 0 && alg != shortAlg {
if len(shortAlg) > 0 && string(alg) != shortAlg {
return false
}
} else if !strings.HasPrefix(hex, shortHex) {
return false
} else if len(shortAlg) > 0 && alg != shortAlg {
} else if len(shortAlg) > 0 && string(alg) != shortAlg {
return false
}
return true
Expand All @@ -68,7 +68,7 @@ func (dst *Set) Lookup(d string) (Digest, error) {
}
var (
searchFunc func(int) bool
alg string
alg Algorithm
hex string
)
dgst, err := ParseDigest(d)
Expand All @@ -88,13 +88,13 @@ func (dst *Set) Lookup(d string) (Digest, error) {
}
}
idx := sort.Search(len(dst.entries), searchFunc)
if idx == len(dst.entries) || !checkShortMatch(dst.entries[idx].alg, dst.entries[idx].val, alg, hex) {
if idx == len(dst.entries) || !checkShortMatch(dst.entries[idx].alg, dst.entries[idx].val, string(alg), hex) {
return "", ErrDigestNotFound
}
if dst.entries[idx].alg == alg && dst.entries[idx].val == hex {
return dst.entries[idx].digest, nil
}
if idx+1 < len(dst.entries) && checkShortMatch(dst.entries[idx+1].alg, dst.entries[idx+1].val, alg, hex) {
if idx+1 < len(dst.entries) && checkShortMatch(dst.entries[idx+1].alg, dst.entries[idx+1].val, string(alg), hex) {
return "", ErrDigestAmbiguous
}

Expand Down Expand Up @@ -172,7 +172,7 @@ func ShortCodeTable(dst *Set, length int) map[Digest]string {
}

type digestEntry struct {
alg string
alg Algorithm
val string
digest Digest
}
Expand Down

0 comments on commit 96bf78c

Please sign in to comment.