blob: aee6cd26cd0b8e3a8b9aa564fd63b394b40036d0 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
|
(***********************************************************************)
(* *)
(* OCaml *)
(* *)
(* Xavier Leroy, projet Cristal, INRIA Rocquencourt *)
(* *)
(* Copyright 1996 Institut National de Recherche en Informatique et *)
(* en Automatique. All rights reserved. This file is distributed *)
(* under the terms of the GNU Library General Public License, with *)
(* the special exception on linking described in file ../LICENSE. *)
(* *)
(***********************************************************************)
(* Message digest (MD5) *)
type t = string
let compare = String.compare
external unsafe_string: string -> int -> int -> t = "caml_md5_string"
external channel: in_channel -> int -> t = "caml_md5_chan"
let string str =
unsafe_string str 0 (String.length str)
let substring str ofs len =
if ofs < 0 || len < 0 || ofs > String.length str - len
then invalid_arg "Digest.substring"
else unsafe_string str ofs len
let file filename =
let ic = open_in_bin filename in
let d = channel ic (-1) in
close_in ic;
d
let output chan digest =
output chan digest 0 16
let input chan =
let digest = String.create 16 in
really_input chan digest 0 16;
digest
let to_hex d =
let result = String.create 32 in
for i = 0 to 15 do
String.blit (Printf.sprintf "%02x" (int_of_char d.[i])) 0 result (2*i) 2;
done;
result
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
let result = String.create 16 in
for i = 0 to 15 do
result.[i] <- Char.chr (byte (2 * i));
done;
result
|