Skip to content

Commit

Permalink
add md5 in OCaml (#40)
Browse files Browse the repository at this point in the history
  • Loading branch information
CodiePP committed Jul 11, 2024
1 parent c91183f commit 0d758c7
Show file tree
Hide file tree
Showing 8 changed files with 107 additions and 5 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,15 @@ 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) |
| KEY256 | [](src/cpp/key256.hpp.md) | [](src/cpp/key256_cbindings.cpp.md) | [?](src/csharp/Key256.cs.md) | [](src/ml/lib/key256.ml) | [?](src/haskell/Key256.hs.md) |
| 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
Expand Down
6 changes: 6 additions & 0 deletions src/ml/bin/dune
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
30 changes: 30 additions & 0 deletions src/ml/bin/md5.ml
Original file line number Diff line number Diff line change
@@ -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 ()
4 changes: 2 additions & 2 deletions src/ml/bin/sha256.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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"
Expand Down
4 changes: 2 additions & 2 deletions src/ml/lib/dune
Original file line number Diff line number Diff line change
Expand Up @@ -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)
)
Expand Down
55 changes: 55 additions & 0 deletions src/ml/lib/md5.cxx
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// OCaml includes
extern "C" {
#include <caml/mlvalues.h>
#include <caml/memory.h>
#include <caml/alloc.h>
// #include <caml/bigarray.h>
// #include <caml/custom.h>
// #include <caml/callback.h>
// #include <caml/fail.h>

#include <sys/errno.h>
} //extern C

// C++ includes
#include <string>
#include <filesystem>

#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
5 changes: 5 additions & 0 deletions src/ml/lib/md5.ml
Original file line number Diff line number Diff line change
@@ -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"
5 changes: 5 additions & 0 deletions src/ml/lib/md5.mli
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@

type b = Mlcpp_cstdio.Cstdio.File.Buffer.ta

val string : string -> string
val buffer : b -> string

0 comments on commit 0d758c7

Please sign in to comment.