Skip to content

Commit 1ec27cc

Browse files
committed
ref(reference): Refactor digest parsing and validation
Don't use a fixed digest algorithm length. Signed-off-by: Benjamin Neff <[email protected]>
1 parent 8834eb3 commit 1ec27cc

File tree

1 file changed

+7
-15
lines changed

1 file changed

+7
-15
lines changed

src/reference.rs

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -256,33 +256,25 @@ impl TryFrom<String> for Reference {
256256
}
257257
// Digests much always be hex-encoded, ensuring that their hex portion will always be
258258
// size*2
259-
if reference.digest().is_some() {
260-
let d = reference.digest().unwrap();
261-
// FIXME: we should actually separate the algorithm from the digest
262-
// using regular expressions. This won't hold up if we support an
263-
// algorithm more or less than 6 characters like sha1024.
264-
if d.len() < 8 {
265-
return Err(ParseError::DigestInvalidFormat);
266-
}
267-
let algo = &d[0..6];
268-
let digest = &d[7..];
269-
match algo {
270-
"sha256" => {
259+
if let Some(digest) = reference.digest() {
260+
match digest.split_once(':') {
261+
None => return Err(ParseError::DigestInvalidFormat),
262+
Some(("sha256", digest)) => {
271263
if digest.len() != 64 {
272264
return Err(ParseError::DigestInvalidLength);
273265
}
274266
}
275-
"sha384" => {
267+
Some(("sha384", digest)) => {
276268
if digest.len() != 96 {
277269
return Err(ParseError::DigestInvalidLength);
278270
}
279271
}
280-
"sha512" => {
272+
Some(("sha512", digest)) => {
281273
if digest.len() != 128 {
282274
return Err(ParseError::DigestInvalidLength);
283275
}
284276
}
285-
_ => return Err(ParseError::DigestUnsupported),
277+
Some((_, _)) => return Err(ParseError::DigestUnsupported),
286278
}
287279
}
288280
Ok(reference)

0 commit comments

Comments
 (0)