Debian Codegraph @

vendor/opam/src/core/opamHash.ml

raw
1 (**************************************************************************)
2 (* *)
3 (* Copyright 2016-2019 OCamlPro *)
4 (* *)
5 (* All rights reserved. This file is distributed under the terms of the *)
6 (* GNU Lesser General Public License version 2.1, with the special *)
7 (* exception on linking described in the file LICENSE. *)
8 (* *)
9 (**************************************************************************)
10
11 type kind = [ `MD5 | `SHA256 | `SHA512 ]
12
13 let default_kind = `MD5
14
15 type t = kind * string
16
17 let kind = fst
18 let contents = snd
19
20 (* Order by hash strength: MD5 < SHA256 < SHA512 *)
21 let compare_kind k l =
22 match k, l with
23 | `SHA512, `SHA512 | `SHA256, `SHA256 | `MD5, `MD5 -> 0
24 | `MD5, _ | _, `SHA512 -> -1
25 | `SHA512, _ | _, `MD5 -> 1
26
27 let compare (k,h) (l,i) =
28 match compare_kind k l with
29 | 0 -> String.compare h i
30 | cmp -> cmp
31
32 let equal h h' = compare h h' = 0
33
34 let pfx_sep_char = '='
35 let pfx_sep_str = String.make 1 pfx_sep_char
36
37 let string_of_kind = function
38 | `MD5 -> "md5"
39 | `SHA256 -> "sha256"
40 | `SHA512 -> "sha512"
41
42 let kind_of_string s = match String.lowercase_ascii s with
43 | "md5" -> `MD5
44 | "sha256" -> `SHA256
45 | "sha512" -> `SHA512
46 | _ -> invalid_arg "OpamHash.kind_of_string"
47
48 let is_hex_str len s =
49 String.length s = len && OpamStd.String.is_hex s
50
51 let len = function
52 | `MD5 -> 32
53 | `SHA256 -> 64
54 | `SHA512 -> 128
55
56 let valid kind = is_hex_str (len kind)
57
58 let is_null h =
59 let count_not_zero c =
60 function '0' -> c | _ -> succ c
61 in
62 OpamStd.String.fold_left count_not_zero 0 (contents h) <> 0
63
64 let make kind s =
65 if valid kind s then kind, String.lowercase_ascii s
66 else invalid_arg ("OpamHash.make_"^string_of_kind kind)
67
68 let md5 = make `MD5
69 let sha256 = make `SHA256
70 let sha512 = make `SHA512
71
72 let of_string_opt s =
73 try
74 let kind, s =
75 match OpamStd.String.cut_at s pfx_sep_char with
76 | None -> `MD5, s
77 | Some (skind, s) -> kind_of_string skind, s
78 in
79 if valid kind s then Some (kind, String.lowercase_ascii s)
80 else None
81 with Invalid_argument _ -> None
82
83 let of_string s =
84 match of_string_opt s with
85 | Some h -> h
86 | None -> invalid_arg "OpamHash.of_string"
87
88 let to_string (kind,s) =
89 String.concat pfx_sep_str [string_of_kind kind; s]
90
91 let to_json s = `String (to_string s)
92 let of_json = function
93 | `String s -> of_string_opt s
94 | _ -> None
95
96 let to_path (kind,s) =
97 [string_of_kind kind; String.sub s 0 2; s]
98
99 let sort checksums =
100 List.sort (fun h h' -> compare h' h) checksums
101
102 let compute ?(kind=default_kind) file = match kind with
103 | `MD5 -> md5 (Digest.to_hex (Digest.file file))
104 | (`SHA256 | `SHA512) as kind -> make kind (OpamSHA.hash_file kind file)
105
106 let compute_from_string ?(kind=default_kind) str = match kind with
107 | `MD5 -> md5 (Digest.to_hex (Digest.string str))
108 | (`SHA256 | `SHA512) as kind -> make kind (OpamSHA.hash_string kind str)
109
110 let check_file f (kind, _ as h) = compute ~kind f = h
111
112 let mismatch f (kind, _ as h) =
113 let hf = compute ~kind f in
114 if hf = h then None else Some hf
115
116 module O = struct
117 type _t = t
118 type t = _t
119 let to_string = to_string
120 let to_json = to_json
121 let of_json = of_json
122 let compare = compare
123 end
124
125 module Set = OpamStd.Set.Make(O)
126
127 module Map = OpamStd.Map.Make(O)