diff --git a/README.md b/README.md index 26987ad..75b9d59 100644 --- a/README.md +++ b/README.md @@ -23,7 +23,7 @@ base library that provides cryptographic functions to _elykseer_ implementations | algo | C++ | C | C# | OCaml | Haskell | | ---- | ---- | ---- | ---- | ---- | ---- | | AES | [√](src/cpp/aes.hpp.md) | [√](src/cpp/aes_cbindings.cpp.md) | [?](src/csharp/Aes.cs.md) | [√](src/ml/lib/aes256.ml) | [?](src/haskell/Aes.hs.md) | -| MD5 | [√](src/cpp/md5.hpp.md) | [√](src/cpp/md5_cbindings.cpp.md) | [?](src/csharp/Md5.cs.md) | [?](src/ml/lib/md5.ml) | [?](src/haskell/Md5.hs.md) | +| MD5 | [√](src/cpp/md5.hpp.md) | [√](src/cpp/md5_cbindings.cpp.md) | [?](src/csharp/Md5.cs.md) | [√](src/ml/lib/md5.ml) | [?](src/haskell/Md5.hs.md) | | SHA256 | [√](src/cpp/sha256.hpp.md) | [√](src/cpp/sha256_cbindings.cpp.md) | [?](src/csharp/Sha256.cs.md) | [√](src/ml/lib/sha256.ml) | [?](src/haskell/Sha256.hs.md) | | KEY128 | [√](src/cpp/key128.hpp.md) | [√](src/cpp/key128_cbindings.cpp.md) | [?](src/csharp/Key128.cs.md) | [√](src/ml/lib/key128.ml) | [?](src/haskell/Key128.hs.md) | | KEY160 | [√](src/cpp/key160.hpp.md) | [√](src/cpp/key160_cbindings.cpp.md) | [?](src/csharp/Key160.cs.md) | [√](src/ml/lib/key160.ml) | [?](src/haskell/Key160.hs.md) | @@ -31,6 +31,7 @@ base library that provides cryptographic functions to _elykseer_ implementations | Random | [√](src/cpp/random.hpp.md) | [?](src/cpp/random_cbindings.cpp.md) | [?](src/csharp/Random.cs.md) | [√](src/ml/lib/random.ml) | [?](src/haskell/Random.hs.md) | | RandomList | [√](src/cpp/randlist.hpp.md) | [?](src/cpp/randlist_cbindings.cpp.md) | [?](src/csharp/RandList.cs.md) | [?](src/ml/lib/randlist.ml) | [?](src/haskell/RandList.hs.md) | | HMAC | [√](src/cpp/hmac.hpp.md) | [√](src/cpp/hmac_cbindings.cpp.md) | [?](src/csharp/HMAC.cs.md) | [√](src/ml/lib/hmac.ml) | [?](src/haskell/HMAC.hs.md) | +| Base64 | [√](src/cpp/base64.hpp.md) | [√](src/cpp/base64_cbindings.cpp.md) | [?](src/csharp/Base64.cs.md) | [√](src/ml/lib/base64.ml) | [?](src/haskell/Base64.hs.md) | # cross compilation diff --git a/src/ml/bin/dune b/src/ml/bin/dune index ea68d7f..75f6dc8 100644 --- a/src/ml/bin/dune +++ b/src/ml/bin/dune @@ -22,6 +22,12 @@ (flags :standard -cclib -lstdc++) (libraries unix elykseer_crypto mlcpp_cstdio mlcpp_filesystem)) +(executable + (name md5) + (modules md5) + (flags :standard -cclib -lstdc++) + (libraries unix elykseer_crypto mlcpp_cstdio mlcpp_filesystem)) + (executable (name sha256) (modules sha256) diff --git a/src/ml/bin/md5.ml b/src/ml/bin/md5.ml new file mode 100644 index 0000000..64ff8c1 --- /dev/null +++ b/src/ml/bin/md5.ml @@ -0,0 +1,30 @@ +open Elykseer_crypto +open Mlcpp_cstdio +open Mlcpp_filesystem + +let msg = "1234567890123456789012345678901234567890123456789012345678901234" + +let read_file fn = + Cstdio.File.fopen fn "rb" |> function + | Ok fptr -> begin + let sz = Filesystem.Path.file_size (Filesystem.Path.from_string fn) in + let buf = Cstdio.File.Buffer.create sz in + Cstdio.File.fread buf sz fptr |> function + | Ok cnt -> + Ok (cnt, buf) + | Error (errno,errstr) -> + Printf.printf "no:%d err:%s\n" errno errstr ; Error (-98) + end + | Error _ -> Error (-99) + +let md5file () = + read_file "/bin/sh" |> function + | Error code -> Printf.printf "Error: %d\n" code |> ignore + | Ok (_cnt,buf) -> + Md5.buffer buf |> Printf.printf "md5 of file: %s\n" + +let md5msg () = + Md5.string msg |> Printf.printf "md5 of msg: %s\n" + + let () = + md5file () ; md5msg () \ No newline at end of file diff --git a/src/ml/bin/sha256.ml b/src/ml/bin/sha256.ml index f4c33ad..58844ea 100644 --- a/src/ml/bin/sha256.ml +++ b/src/ml/bin/sha256.ml @@ -5,7 +5,7 @@ open Mlcpp_filesystem let msg = "1234567890123456789012345678901234567890123456789012345678901234" let read_file fn = - Cstdio.File.fopen fn "r" |> function + Cstdio.File.fopen fn "rb" |> function | Ok fptr -> begin let sz = Filesystem.Path.file_size (Filesystem.Path.from_string fn) in let buf = Cstdio.File.Buffer.create sz in @@ -18,7 +18,7 @@ let read_file fn = | Error _ -> Error (-99) let shafile () = - read_file "/tmp/test.txt" |> function + read_file "/bin/sh" |> function | Error code -> Printf.printf "Error: %d\n" code |> ignore | Ok (_cnt,buf) -> Sha256.buffer buf |> Printf.printf "sha256 of file: %s\n" diff --git a/src/ml/lib/dune b/src/ml/lib/dune index 658ded0..c6f594a 100644 --- a/src/ml/lib/dune +++ b/src/ml/lib/dune @@ -4,10 +4,10 @@ (preprocess (pps ppx_optcomp)) (no_dynlink) - (modules key128 key160 key256 aes256 random sha256 hmac base64) + (modules key128 key160 key256 aes256 random md5 sha256 hmac base64) (c_library_flags :standard -lstdc++) (foreign_stubs (language cxx) (flags :standard -I ../../../../../ext -I ../../../../../build/src -std=c++20 -fno-exceptions) - (names key128 key160 key256 aes256 random sha256 hmac base64)) + (names key128 key160 key256 aes256 random md5 sha256 hmac base64)) (foreign_archives elykseer-crypto_cpp) (libraries unix mlcpp_cstdio) ) diff --git a/src/ml/lib/md5.cxx b/src/ml/lib/md5.cxx new file mode 100644 index 0000000..40b70ec --- /dev/null +++ b/src/ml/lib/md5.cxx @@ -0,0 +1,55 @@ +// OCaml includes +extern "C" { +#include +#include +#include +// #include +// #include +// #include +// #include + +#include +} //extern C + +// C++ includes +#include +#include + +#include "lxr/key128.hpp" +#include "lxr/md5.hpp" + +using namespace lxr; + +struct _cpp_cstdio_buffer { + char *_buf {nullptr}; + long _len {0}; +}; + +#define CPP_CSTDIO_BUFFER(v) (*((_cpp_cstdio_buffer**) Data_custom_val(v))) + +/* + * cpp_string_md5 : string -> string + */ +extern "C" { +value cpp_string_md5(value vs) +{ + CAMLparam1(vs); + const char *s = String_val(vs); + const int len = caml_string_length(vs); + auto k = lxr::Md5::hash(s, len); + CAMLreturn(caml_copy_string(k.toHex().c_str())); +} +} // extern C + +/* + * cpp_buffer_md5 : buffer -> string + */ +extern "C" { +value cpp_buffer_md5(value vb) +{ + CAMLparam1(vb); + struct _cpp_cstdio_buffer *b = CPP_CSTDIO_BUFFER(vb); + auto k = lxr::Md5::hash(b->_buf, b->_len); + CAMLreturn(caml_copy_string(k.toHex().c_str())); +} +} // extern C diff --git a/src/ml/lib/md5.ml b/src/ml/lib/md5.ml new file mode 100644 index 0000000..a0c2034 --- /dev/null +++ b/src/ml/lib/md5.ml @@ -0,0 +1,5 @@ + +type b = Mlcpp_cstdio.Cstdio.File.Buffer.ta + +external string : string -> string = "cpp_string_md5" +external buffer : b -> string = "cpp_buffer_md5" diff --git a/src/ml/lib/md5.mli b/src/ml/lib/md5.mli new file mode 100644 index 0000000..5c15b2f --- /dev/null +++ b/src/ml/lib/md5.mli @@ -0,0 +1,5 @@ + +type b = Mlcpp_cstdio.Cstdio.File.Buffer.ta + +val string : string -> string +val buffer : b -> string