1995-10-17 03:01:45 -07:00
|
|
|
(***********************************************************************)
|
|
|
|
(* *)
|
2011-07-27 07:17:02 -07:00
|
|
|
(* OCaml *)
|
1995-10-17 03:01:45 -07:00
|
|
|
(* *)
|
|
|
|
(* Xavier Leroy, projet Cristal, INRIA Rocquencourt *)
|
|
|
|
(* *)
|
1996-04-30 07:53:58 -07:00
|
|
|
(* Copyright 1996 Institut National de Recherche en Informatique et *)
|
1999-11-17 10:59:06 -08:00
|
|
|
(* en Automatique. All rights reserved. This file is distributed *)
|
2001-12-07 05:41:02 -08:00
|
|
|
(* under the terms of the GNU Library General Public License, with *)
|
|
|
|
(* the special exception on linking described in file ../LICENSE. *)
|
1995-10-17 03:01:45 -07:00
|
|
|
(* *)
|
|
|
|
(***********************************************************************)
|
|
|
|
|
|
|
|
(* Message digest (MD5) *)
|
|
|
|
|
|
|
|
type t = string
|
|
|
|
|
2011-12-28 19:10:49 -08:00
|
|
|
let compare = String.compare
|
|
|
|
|
2003-12-31 06:20:40 -08:00
|
|
|
external unsafe_string: string -> int -> int -> t = "caml_md5_string"
|
|
|
|
external channel: in_channel -> int -> t = "caml_md5_chan"
|
1995-10-17 03:01:45 -07:00
|
|
|
|
|
|
|
let string str =
|
|
|
|
unsafe_string str 0 (String.length str)
|
|
|
|
|
2014-05-01 14:54:15 -07:00
|
|
|
let bytes b = string (Bytes.unsafe_to_string b)
|
|
|
|
|
1995-10-17 03:01:45 -07:00
|
|
|
let substring str ofs len =
|
2002-07-12 02:47:54 -07:00
|
|
|
if ofs < 0 || len < 0 || ofs > String.length str - len
|
1997-04-11 06:57:48 -07:00
|
|
|
then invalid_arg "Digest.substring"
|
1995-10-17 03:01:45 -07:00
|
|
|
else unsafe_string str ofs len
|
|
|
|
|
2014-05-01 14:54:15 -07:00
|
|
|
let subbytes b ofs len = substring (Bytes.unsafe_to_string b) ofs len
|
|
|
|
|
1995-10-17 03:01:45 -07:00
|
|
|
let file filename =
|
|
|
|
let ic = open_in_bin filename in
|
2002-02-25 08:37:15 -08:00
|
|
|
let d = channel ic (-1) in
|
1995-10-17 03:01:45 -07:00
|
|
|
close_in ic;
|
|
|
|
d
|
|
|
|
|
|
|
|
let output chan digest =
|
2014-04-29 04:56:17 -07:00
|
|
|
output_string chan digest
|
1995-10-17 03:01:45 -07:00
|
|
|
|
2014-04-29 04:56:17 -07:00
|
|
|
let input chan = really_input_string chan 16
|
2002-02-25 08:37:15 -08:00
|
|
|
|
2014-04-12 03:17:02 -07:00
|
|
|
let char_hex n =
|
|
|
|
Char.unsafe_chr (n + if n < 10 then Char.code '0' else (Char.code 'a' - 10))
|
2013-08-04 12:58:07 -07:00
|
|
|
|
2002-02-25 08:37:15 -08:00
|
|
|
let to_hex d =
|
2014-04-29 04:56:17 -07:00
|
|
|
let result = Bytes.create 32 in
|
2002-02-25 08:37:15 -08:00
|
|
|
for i = 0 to 15 do
|
2013-08-04 12:58:07 -07:00
|
|
|
let x = Char.code d.[i] in
|
2014-04-29 04:56:17 -07:00
|
|
|
Bytes.unsafe_set result (i*2) (char_hex (x lsr 4));
|
|
|
|
Bytes.unsafe_set result (i*2+1) (char_hex (x land 0x0f));
|
2002-02-25 08:37:15 -08:00
|
|
|
done;
|
2014-04-29 04:56:17 -07:00
|
|
|
Bytes.unsafe_to_string result
|
2012-01-26 14:56:48 -08:00
|
|
|
|
|
|
|
let from_hex s =
|
|
|
|
if String.length s <> 32 then raise (Invalid_argument "Digest.from_hex");
|
|
|
|
let digit c =
|
|
|
|
match c with
|
|
|
|
| '0'..'9' -> Char.code c - Char.code '0'
|
|
|
|
| 'A'..'F' -> Char.code c - Char.code 'A' + 10
|
|
|
|
| 'a'..'f' -> Char.code c - Char.code 'a' + 10
|
|
|
|
| _ -> raise (Invalid_argument "Digest.from_hex")
|
|
|
|
in
|
|
|
|
let byte i = digit s.[i] lsl 4 + digit s.[i+1] in
|
2014-04-29 04:56:17 -07:00
|
|
|
let result = Bytes.create 16 in
|
2012-01-26 14:56:48 -08:00
|
|
|
for i = 0 to 15 do
|
2014-04-29 04:56:17 -07:00
|
|
|
Bytes.set result i (Char.chr (byte (2 * i)));
|
2012-01-26 14:56:48 -08:00
|
|
|
done;
|
2014-04-29 04:56:17 -07:00
|
|
|
Bytes.unsafe_to_string result
|