16 fun hex_string arr i = |
16 fun hex_string arr i = |
17 let val c = CInterface.fromCchar (CInterface.offset i CInterface.Cchar arr) |
17 let val c = CInterface.fromCchar (CInterface.offset i CInterface.Cchar arr) |
18 in (op ^) (apply2 hex_digit (Integer.div_mod (Char.ord c) 16)) end |
18 in (op ^) (apply2 hex_digit (Integer.div_mod (Char.ord c) 16)) end |
19 |
19 |
20 val lib_path = |
20 val lib_path = |
21 ("$ML_HOME/" ^ (if ML_System.platform_is_cygwin then "sha1.dll" else "libsha1.so")) |
21 ("$ML_HOME/" ^ |
|
22 (if ML_System.platform_is_cygwin orelse ML_System.platform_is_windows |
|
23 then "sha1.dll" else "libsha1.so")) |
22 |> Path.explode; |
24 |> Path.explode; |
23 |
25 |
24 val STRING_INPUT_BYTES = |
26 val STRING_INPUT_BYTES = |
25 CInterface.mkConversion undefined (CInterface.toCbytes o Byte.stringToBytes) |
27 CInterface.mkConversion undefined (CInterface.toCbytes o Byte.stringToBytes) |
26 (CInterface.Cpointer CInterface.Cchar); |
28 (CInterface.Cpointer CInterface.Cchar); |
27 |
29 |
28 fun digest_external str = |
30 fun digest_external str = |
29 let |
31 let |
30 val digest = CInterface.alloc 20 CInterface.Cchar; |
32 val digest = CInterface.alloc 20 CInterface.Cchar; |
31 val _ = |
33 val _ = |
32 CInterface.call3 (CInterface.get_sym (File.platform_path lib_path) "sha1_buffer") |
34 CInterface.call3 |
|
35 (CInterface.get_sym (ml_platform_path (File.platform_path lib_path)) "sha1_buffer") |
33 (STRING_INPUT_BYTES, CInterface.LONG, CInterface.POINTER) |
36 (STRING_INPUT_BYTES, CInterface.LONG, CInterface.POINTER) |
34 CInterface.POINTER (str, size str, CInterface.address digest); |
37 CInterface.POINTER (str, size str, CInterface.address digest); |
35 in fold (suffix o hex_string digest) (0 upto 19) "" end; |
38 in fold (suffix o hex_string digest) (0 upto 19) "" end; |
36 |
39 |
37 fun digest_string str = digest_external str |
40 fun digest_string str = digest_external str |