Skip to content

Commit 55f6758

Browse files
committed
digest: update package methods to reflect changes
We made a change to call the _hex_ portion the _encoded_ portion and this makes a few updates to make that clearer for prospective users of this package. Signed-off-by: Stephen J Day <[email protected]>
1 parent 5ab10f5 commit 55f6758

3 files changed

Lines changed: 31 additions & 13 deletions

File tree

algorithm.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,14 @@ func (a Algorithm) Hash() hash.Hash {
125125
return algorithms[a].New()
126126
}
127127

128+
// Encode encodes the raw bytes of a digest, typically from a hash.Hash, into
129+
// the encoded portion of the digest.
130+
func (a Algorithm) Encode(d []byte) string {
131+
// TODO(stevvooe): Currently, all algorithms use a hex encoding. When we
132+
// add support for back registration, we can modify this accordingly.
133+
return fmt.Sprintf("%x", d)
134+
}
135+
128136
// FromReader returns the digest of the reader using the algorithm.
129137
func (a Algorithm) FromReader(rd io.Reader) (Digest, error) {
130138
digester := a.Digester()

digest.go

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,17 @@ func NewDigest(alg Algorithm, h hash.Hash) Digest {
4545
// functions. This is also useful for rebuilding digests from binary
4646
// serializations.
4747
func NewDigestFromBytes(alg Algorithm, p []byte) Digest {
48-
return Digest(fmt.Sprintf("%s:%x", alg, p))
48+
return NewDigestFromEncoded(alg, alg.Encode(p))
4949
}
5050

51-
// NewDigestFromHex returns a Digest from alg and a the hex encoded digest.
51+
// NewDigestFromHex is deprecated. Please use NewDigestFromEncoded.
5252
func NewDigestFromHex(alg, hex string) Digest {
53-
return Digest(fmt.Sprintf("%s:%s", alg, hex))
53+
return NewDigestFromEncoded(Algorithm(alg), hex)
54+
}
55+
56+
// NewDigestFromEncoded returns a Digest from alg and the encoded digest.
57+
func NewDigestFromEncoded(alg Algorithm, encoded string) Digest {
58+
return Digest(fmt.Sprintf("%s:%s", alg, encoded))
5459
}
5560

5661
// DigestRegexp matches valid digest types.
@@ -133,12 +138,17 @@ func (d Digest) Verifier() Verifier {
133138
}
134139
}
135140

136-
// Hex returns the hex digest portion of the digest. This will panic if the
141+
// Encoded returns the encoded portion of the digest. This will panic if the
137142
// underlying digest is not in a valid format.
138-
func (d Digest) Hex() string {
143+
func (d Digest) Encoded() string {
139144
return string(d[d.sepIndex()+1:])
140145
}
141146

147+
// Hex is deprecated. Please use Digest.Encoded.
148+
func (d Digest) Hex() string {
149+
return d.Encoded()
150+
}
151+
142152
func (d Digest) String() string {
143153
return string(d)
144154
}

digest_test.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,17 @@ func TestParseDigest(t *testing.T) {
2323
input string
2424
err error
2525
algorithm Algorithm
26-
hex string
26+
encoded string
2727
}{
2828
{
2929
input: "sha256:e58fcf7418d4390dec8e8fb69d88c06ec07039d651fedd3aa72af9972e7d046b",
3030
algorithm: "sha256",
31-
hex: "e58fcf7418d4390dec8e8fb69d88c06ec07039d651fedd3aa72af9972e7d046b",
31+
encoded: "e58fcf7418d4390dec8e8fb69d88c06ec07039d651fedd3aa72af9972e7d046b",
3232
},
3333
{
3434
input: "sha384:d3fc7881460b7e22e3d172954463dddd7866d17597e7248453c48b3e9d26d9596bf9c4a9cf8072c9d5bad76e19af801d",
3535
algorithm: "sha384",
36-
hex: "d3fc7881460b7e22e3d172954463dddd7866d17597e7248453c48b3e9d26d9596bf9c4a9cf8072c9d5bad76e19af801d",
36+
encoded: "d3fc7881460b7e22e3d172954463dddd7866d17597e7248453c48b3e9d26d9596bf9c4a9cf8072c9d5bad76e19af801d",
3737
},
3838
{
3939
// empty hex
@@ -78,13 +78,13 @@ func TestParseDigest(t *testing.T) {
7878
// ensure that we parse, but we don't have support for the algorithm
7979
input: "sha384.foo+bar:d3fc7881460b7e22e3d172954463dddd7866d17597e7248453c48b3e9d26d9596bf9c4a9cf8072c9d5bad76e19af801d",
8080
algorithm: "sha384.foo+bar",
81-
hex: "d3fc7881460b7e22e3d172954463dddd7866d17597e7248453c48b3e9d26d9596bf9c4a9cf8072c9d5bad76e19af801d",
81+
encoded: "d3fc7881460b7e22e3d172954463dddd7866d17597e7248453c48b3e9d26d9596bf9c4a9cf8072c9d5bad76e19af801d",
8282
err: ErrDigestUnsupported,
8383
},
8484
{
8585
input: "sha384_foo+bar:d3fc7881460b7e22e3d172954463dddd7866d17597e7248453c48b3e9d26d9596bf9c4a9cf8072c9d5bad76e19af801d",
8686
algorithm: "sha384_foo+bar",
87-
hex: "d3fc7881460b7e22e3d172954463dddd7866d17597e7248453c48b3e9d26d9596bf9c4a9cf8072c9d5bad76e19af801d",
87+
encoded: "d3fc7881460b7e22e3d172954463dddd7866d17597e7248453c48b3e9d26d9596bf9c4a9cf8072c9d5bad76e19af801d",
8888
err: ErrDigestUnsupported,
8989
},
9090
} {
@@ -101,8 +101,8 @@ func TestParseDigest(t *testing.T) {
101101
t.Fatalf("incorrect algorithm for parsed digest: %q != %q", digest.Algorithm(), testcase.algorithm)
102102
}
103103

104-
if digest.Hex() != testcase.hex {
105-
t.Fatalf("incorrect hex for parsed digest: %q != %q", digest.Hex(), testcase.hex)
104+
if digest.Encoded() != testcase.encoded {
105+
t.Fatalf("incorrect hex for parsed digest: %q != %q", digest.Encoded(), testcase.encoded)
106106
}
107107

108108
// Parse string return value and check equality
@@ -116,7 +116,7 @@ func TestParseDigest(t *testing.T) {
116116
t.Fatalf("expected equal: %q != %q", newParsed, digest)
117117
}
118118

119-
newFromHex := NewDigestFromHex(newParsed.Algorithm().String(), newParsed.Hex())
119+
newFromHex := NewDigestFromEncoded(newParsed.Algorithm(), newParsed.Encoded())
120120
if newFromHex != digest {
121121
t.Fatalf("%v != %v", newFromHex, digest)
122122
}

0 commit comments

Comments
 (0)