Skip to content

Commit 50bd59c

Browse files
committed
Simpler md5 parsing
Signed-off-by: Sebastian Rabenhorst <[email protected]>
1 parent 84b8e0e commit 50bd59c

File tree

2 files changed

+9
-18
lines changed

2 files changed

+9
-18
lines changed

clientutil/parse.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -66,14 +66,15 @@ func ParseLastModified(m http.Header, f string) (time.Time, error) {
6666
return mod, nil
6767
}
6868

69-
// ParseMD5 returns the bytes parsed from the hex-encoded MD5 string.
69+
// ParseMD5 returns the bytes parsed from the hex-encoded MD5 string
7070
// It trims potential surrounding double quotes before decoding.
71-
func ParseMD5(md5Hex string) ([]byte, error) {
71+
// It returns nil if the MD5 string is not valid.
72+
func ParseMD5(md5Hex string) []byte {
7273
// Trim surrounding double quotes if present.
7374
trimmed := strings.Trim(md5Hex, "\"")
74-
decoded, err := hex.DecodeString(trimmed)
75-
if err != nil {
76-
return nil, errors.Wrapf(err, "decode hex MD5 string: %s", md5Hex)
75+
decoded, _ := hex.DecodeString(trimmed)
76+
if len(decoded) != 16 {
77+
return nil
7778
}
78-
return decoded, nil
79+
return decoded
7980
}

clientutil/parse_test.go

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,6 @@ func TestParseMD5(t *testing.T) {
116116
label string
117117
in string
118118
out []byte
119-
err string
120119
}{
121120
{
122121
label: "valid md5",
@@ -126,12 +125,10 @@ func TestParseMD5(t *testing.T) {
126125
{
127126
label: "invalid hex string",
128127
in: "not-a-hex-string",
129-
err: "decode hex MD5 string: not-a-hex-string: encoding/hex: invalid byte: U+006E 'n'",
130128
},
131129
{
132130
label: "odd length hex string",
133131
in: "abc",
134-
err: "decode hex MD5 string: abc: encoding/hex: odd length hex string",
135132
},
136133
{
137134
label: "empty string",
@@ -146,7 +143,6 @@ func TestParseMD5(t *testing.T) {
146143
{
147144
label: "invalid hex string with quotes",
148145
in: "\"not-a-hex-string\"",
149-
err: "decode hex MD5 string: \"not-a-hex-string\": encoding/hex: invalid byte: U+006E 'n'",
150146
},
151147
{
152148
label: "only quotes",
@@ -155,14 +151,8 @@ func TestParseMD5(t *testing.T) {
155151
},
156152
} {
157153
t.Run(tc.label, func(t *testing.T) {
158-
out, err := ParseMD5(tc.in)
159-
if tc.err != "" {
160-
testutil.Assert(t, err.Error() == tc.err, fmt.Sprintf("error message mismatch: %s != %s", err.Error(), tc.err))
161-
testutil.Assert(t, out == nil, "output should be nil")
162-
} else {
163-
testutil.Ok(t, err)
164-
testutil.Assert(t, bytes.Equal(out, tc.out), fmt.Sprintf("output mismatch: %v != %v", out, tc.out))
165-
}
154+
out := ParseMD5(tc.in)
155+
testutil.Assert(t, bytes.Equal(out, tc.out), fmt.Sprintf("output mismatch: %v != %v", out, tc.out))
166156
})
167157
}
168158
}

0 commit comments

Comments
 (0)