src/Pure/General/sha1_polyml.ML
author wenzelm
Sat, 13 Mar 2010 14:44:47 +0100
changeset 35743 c506c029a082
parent 35713 428284ee1465
child 35935 32887cbfd62d
permissions -rw-r--r--
adapted to localized typedef: handle single global interpretation only;

(*  Title:      Pure/General/sha1_polyml.ML
    Author:     Sascha Boehme, TU Muenchen

Digesting strings according to SHA-1 (see RFC 3174) -- based on an
external implementation in C with a fallback to an internal
implementation.
*)

structure SHA1: SHA1 =
struct

fun hex_digit i = if i < 10 then chr (ord "0" + i) else chr (ord "a" + i - 10);

fun hex_string arr i =
  let val c = CInterface.fromCchar (CInterface.offset i CInterface.Cchar arr)
  in (op ^) (pairself hex_digit (Integer.div_mod (Char.ord c) 16)) end

val lib_path =
  ("$ML_HOME/" ^ (if String.isSuffix "cygwin" ml_system then "sha1.dll" else "libsha1.so"))
  |> Path.explode;

fun digest_external str =
  let
    val digest = CInterface.alloc 20 CInterface.Cchar;
    val _ =
      CInterface.call3 (CInterface.get_sym (File.platform_path lib_path) "sha1_buffer")
        (CInterface.STRING, CInterface.INT, CInterface.POINTER)
        CInterface.POINTER (str, size str, CInterface.address digest);
  in fold (suffix o hex_string digest) (0 upto 19) "" end;

fun digest str = digest_external str
  handle CInterface.Foreign msg =>
    (warning (msg ^ "\nUsing slow ML implementation of SHA1.digest"); SHA1.digest str);

end;