-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add Sha2-256 in OCaml and rename previous Sha256 to Sha3_256 (#42)
- Loading branch information
Showing
7 changed files
with
122 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 () |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |