From a58dff49c501db3d7d357541494cc6683a13b77c Mon Sep 17 00:00:00 2001 From: Pino Toscano Date: Fri, 30 Sep 2016 11:04:27 +0200 Subject: [PATCH] mllib, builder: add and use Checksums.of_string Add a simple way to turn a combination of checksum type and value into a csum_t. Use it in builder, even if still constrained. --- builder/simplestreams_parser.ml | 8 ++++++-- mllib/checksums.ml | 6 ++++++ mllib/checksums.mli | 6 ++++++ 3 files changed, 18 insertions(+), 2 deletions(-) diff --git a/builder/simplestreams_parser.ml b/builder/simplestreams_parser.ml index 13e0b5d2b..f7682cde3 100644 --- a/builder/simplestreams_parser.ml +++ b/builder/simplestreams_parser.ml @@ -156,8 +156,12 @@ let get_index ~downloader ~sigchecker let checksums = let checksums = object_find_objects ( function - | ("sha256", Yajl_string c) -> Some (Checksums.SHA256 c) - | ("sha512", Yajl_string c) -> Some (Checksums.SHA512 c) + (* Since this catches all the keys, and not just + * the ones related to checksums, explicitly filter + * the supported checksums. + *) + | ("sha256"|"sha512" as t, Yajl_string c) -> + Some (Checksums.of_string t c) | _ -> None ) disk_item in match checksums with diff --git a/mllib/checksums.ml b/mllib/checksums.ml index 918a1c2ba..014e73e80 100644 --- a/mllib/checksums.ml +++ b/mllib/checksums.ml @@ -35,6 +35,12 @@ let string_of_csum = function | SHA256 c -> c | SHA512 c -> c +let of_string csum_type csum_value = + match String.lowercase_ascii csum_type with + | "sha256" -> SHA256 csum_value + | "sha512" -> SHA512 csum_value + | _ -> invalid_arg csum_type + let verify_checksum csum filename = let prog, csum_ref = match csum with diff --git a/mllib/checksums.mli b/mllib/checksums.mli index 202bdd17b..298d7df6b 100644 --- a/mllib/checksums.mli +++ b/mllib/checksums.mli @@ -22,6 +22,12 @@ type csum_t = exception Mismatched_checksum of (csum_t * string) (* expected checksum, got *) +val of_string : string -> string -> csum_t +(** [of_string type value] returns the [csum_t] for the specified + combination of checksum type and checksum value. + + Raise [Invalid_argument] if the checksum type is not known. *) + val verify_checksum : csum_t -> string -> unit (** Verify the checksum of the file. *)