13 |
13 |
14 fun hex_string arr i = |
14 fun hex_string arr i = |
15 let val c = CInterface.fromCchar (CInterface.offset i CInterface.Cchar arr) |
15 let val c = CInterface.fromCchar (CInterface.offset i CInterface.Cchar arr) |
16 in (op ^) (pairself hex_digit (Integer.div_mod (Char.ord c) 16)) end |
16 in (op ^) (pairself hex_digit (Integer.div_mod (Char.ord c) 16)) end |
17 |
17 |
|
18 val lib_path = |
|
19 ("$ML_HOME/" ^ (if String.isSuffix "cygwin" ml_system then "sha1.dll" else "libsha1.so")) |
|
20 |> Path.explode; |
|
21 |
18 fun digest_external str = |
22 fun digest_external str = |
19 let |
23 let |
20 val digest = CInterface.alloc 20 CInterface.Cchar; |
24 val digest = CInterface.alloc 20 CInterface.Cchar; |
21 val _ = CInterface.call3 (CInterface.get_sym "sha1" "sha1_buffer") |
25 val _ = |
22 (CInterface.STRING, CInterface.INT, CInterface.POINTER) |
26 CInterface.call3 (CInterface.get_sym (File.platform_path lib_path) "sha1_buffer") |
23 CInterface.POINTER (str, size str, CInterface.address digest); |
27 (CInterface.STRING, CInterface.INT, CInterface.POINTER) |
|
28 CInterface.POINTER (str, size str, CInterface.address digest); |
24 in fold (suffix o hex_string digest) (0 upto 19) "" end; |
29 in fold (suffix o hex_string digest) (0 upto 19) "" end; |
25 |
30 |
26 fun digest str = digest_external str |
31 fun digest str = digest_external str |
27 handle CInterface.Foreign _ => SHA1.digest str; |
32 handle CInterface.Foreign msg => |
|
33 (warning (msg ^ "\nUsing slow ML implementation of SHA1.digest"); SHA1.digest str); |
28 |
34 |
29 end; |
35 end; |