author | wenzelm |
Mon, 15 Feb 2016 14:55:44 +0100 | |
changeset 62337 | d3996d5873dd |
parent 60989 | c967d423953a |
permissions | -rw-r--r-- |
35628 | 1 |
(* Title: Pure/General/sha1_polyml.ML |
2 |
Author: Sascha Boehme, TU Muenchen |
|
3 |
||
4 |
Digesting strings according to SHA-1 (see RFC 3174) -- based on an |
|
5 |
external implementation in C with a fallback to an internal |
|
6 |
implementation. |
|
7 |
*) |
|
8 |
||
9 |
structure SHA1: SHA1 = |
|
10 |
struct |
|
11 |
||
41954 | 12 |
(* digesting *) |
13 |
||
35628 | 14 |
fun hex_digit i = if i < 10 then chr (ord "0" + i) else chr (ord "a" + i - 10); |
15 |
||
16 |
fun hex_string arr i = |
|
17 |
let val c = CInterface.fromCchar (CInterface.offset i CInterface.Cchar arr) |
|
59058
a78612c67ec0
renamed "pairself" to "apply2", in accordance to @{apply 2};
wenzelm
parents:
57638
diff
changeset
|
18 |
in (op ^) (apply2 hex_digit (Integer.div_mod (Char.ord c) 16)) end |
35628 | 19 |
|
35713
428284ee1465
absolute lib_path relative to ML_HOME -- for improved robustness;
wenzelm
parents:
35628
diff
changeset
|
20 |
val lib_path = |
60989 | 21 |
("$ML_HOME/" ^ (if ML_System.platform_is_windows then "sha1.dll" else "libsha1.so")) |
35713
428284ee1465
absolute lib_path relative to ML_HOME -- for improved robustness;
wenzelm
parents:
35628
diff
changeset
|
22 |
|> Path.explode; |
428284ee1465
absolute lib_path relative to ML_HOME -- for improved robustness;
wenzelm
parents:
35628
diff
changeset
|
23 |
|
53211
753b9fbe18be
allow NUL characters in ML string passed to C library;
wenzelm
parents:
43948
diff
changeset
|
24 |
val STRING_INPUT_BYTES = |
753b9fbe18be
allow NUL characters in ML string passed to C library;
wenzelm
parents:
43948
diff
changeset
|
25 |
CInterface.mkConversion undefined (CInterface.toCbytes o Byte.stringToBytes) |
753b9fbe18be
allow NUL characters in ML string passed to C library;
wenzelm
parents:
43948
diff
changeset
|
26 |
(CInterface.Cpointer CInterface.Cchar); |
753b9fbe18be
allow NUL characters in ML string passed to C library;
wenzelm
parents:
43948
diff
changeset
|
27 |
|
35628 | 28 |
fun digest_external str = |
29 |
let |
|
30 |
val digest = CInterface.alloc 20 CInterface.Cchar; |
|
35713
428284ee1465
absolute lib_path relative to ML_HOME -- for improved robustness;
wenzelm
parents:
35628
diff
changeset
|
31 |
val _ = |
60964
fdb82e722f8a
keep native CInterface to make SHA1 work properly;
wenzelm
parents:
59058
diff
changeset
|
32 |
CInterface.call3 |
60970
e08d868ceca9
clarified File.standard_path vs. File.platform_path (like Isabelle/Scala operations);
wenzelm
parents:
60964
diff
changeset
|
33 |
(CInterface.get_sym (File.platform_path lib_path) "sha1_buffer") |
53211
753b9fbe18be
allow NUL characters in ML string passed to C library;
wenzelm
parents:
43948
diff
changeset
|
34 |
(STRING_INPUT_BYTES, CInterface.LONG, CInterface.POINTER) |
35713
428284ee1465
absolute lib_path relative to ML_HOME -- for improved robustness;
wenzelm
parents:
35628
diff
changeset
|
35 |
CInterface.POINTER (str, size str, CInterface.address digest); |
35628 | 36 |
in fold (suffix o hex_string digest) (0 upto 19) "" end; |
37 |
||
41954 | 38 |
fun digest_string str = digest_external str |
35713
428284ee1465
absolute lib_path relative to ML_HOME -- for improved robustness;
wenzelm
parents:
35628
diff
changeset
|
39 |
handle CInterface.Foreign msg => |
41954 | 40 |
(warning (msg ^ "\nUsing slow ML implementation of SHA1.digest"); SHA1.rep (SHA1.digest str)); |
41 |
||
42 |
||
43 |
(* type digest *) |
|
44 |
||
45 |
datatype digest = Digest of string; |
|
46 |
||
47 |
val digest = Digest o digest_string; |
|
48 |
fun rep (Digest s) = s; |
|
35628 | 49 |
|
57638
ed58e740a699
less authentic SHA1.digest: trust Scala side on blobs and avoid re-calculation via Foreign Language Interface, which might be a cause of problems;
wenzelm
parents:
53211
diff
changeset
|
50 |
val fake = Digest; |
ed58e740a699
less authentic SHA1.digest: trust Scala side on blobs and avoid re-calculation via Foreign Language Interface, which might be a cause of problems;
wenzelm
parents:
53211
diff
changeset
|
51 |
|
35628 | 52 |
end; |