Skip to content

Commit

Permalink
add Sha2-256 in OCaml and rename previous Sha256 to Sha3_256 (#42)
Browse files Browse the repository at this point in the history
  • Loading branch information
CodiePP committed Jul 12, 2024
2 parents b5352ae + 65891dc commit 254e15f
Show file tree
Hide file tree
Showing 7 changed files with 122 additions and 2 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ base library that provides cryptographic functions to _elykseer_ implementations
| 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) |
| 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) |
| SHA3-256 | [](src/cpp/sha3.hpp.md) | [](src/cpp/sha3_cbindings.cpp.md) | [?](src/csharp/Sha3.cs.md) | [](src/ml/lib/sha3_256.ml) | [?](src/haskell/Sha3.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) |
Expand Down
6 changes: 6 additions & 0 deletions src/ml/bin/dune
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@
(flags :standard -cclib -lstdc++)
(libraries unix elykseer_crypto mlcpp_cstdio mlcpp_filesystem))

(executable
(name sha3_256)
(modules sha3_256)
(flags :standard -cclib -lstdc++)
(libraries unix elykseer_crypto mlcpp_cstdio mlcpp_filesystem))

(executable
(name random)
(modules random)
Expand Down
33 changes: 33 additions & 0 deletions src/ml/bin/sha3_256.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
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 sha3file () =
read_file "/bin/sh" |> function
| Error code -> Printf.printf "Error: %d\n" code |> ignore
| Ok (_cnt,buf) ->
Sha3_256.buffer buf |> Printf.printf "sha3_256 of file: %s\n"

let sha3msg () =
Sha3_256.string msg |> Printf.printf "sha3_256 of msg: %s\n"

let sha3path () =
Sha3_256.file "/bin/sh" |> Printf.printf "sha3_256 of path: %s\n"

let () =
sha3file () ; sha3msg () ; sha3path ()
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 md5 sha256 hmac base64)
(modules key128 key160 key256 aes256 random md5 sha256 sha3_256 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 md5 sha256 hmac base64))
(names key128 key160 key256 aes256 random md5 sha256 sha3_256 hmac base64))
(foreign_archives elykseer-crypto_cpp)
(libraries unix mlcpp_cstdio)
)
Expand Down
68 changes: 68 additions & 0 deletions src/ml/lib/sha3_256.cxx
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
// 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/key256.hpp"
#include "lxr/sha3.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_sha3_256 : string -> string
*/
extern "C" {
value cpp_string_sha3_256(value vs)
{
CAMLparam1(vs);
const char *s = String_val(vs);
const int len = caml_string_length(vs);
auto k = lxr::Sha3_256::hash(s, len);
CAMLreturn(caml_copy_string(k.toHex().c_str()));
}
} // extern C

/*
* cpp_file_sha3_256 : string -> string
*/
extern "C" {
value cpp_file_sha3_256(value vfp)
{
CAMLparam1(vfp);
const char *fp = String_val(vfp);
auto k = lxr::Sha3_256::hash(std::filesystem::path(fp));
CAMLreturn(caml_copy_string(k.toHex().c_str()));
}
} // extern C

/*
* cpp_buffer_sha3_256 : buffer -> string
*/
extern "C" {
value cpp_buffer_sha3_256(value vb)
{
CAMLparam1(vb);
struct _cpp_cstdio_buffer *b = CPP_CSTDIO_BUFFER(vb);
auto k = lxr::Sha3_256::hash(b->_buf, b->_len);
CAMLreturn(caml_copy_string(k.toHex().c_str()));
}
} // extern C
6 changes: 6 additions & 0 deletions src/ml/lib/sha3_256.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@

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

external string : string -> string = "cpp_string_sha3_256"
external file : string -> string = "cpp_file_sha3_256"
external buffer : b -> string = "cpp_buffer_sha3_256"
6 changes: 6 additions & 0 deletions src/ml/lib/sha3_256.mli
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@

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

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

0 comments on commit 254e15f

Please sign in to comment.