Created
September 23, 2021 15:58
-
-
Save emillon/9e8bc8f785dbd0b2d104898cfd137469 to your computer and use it in GitHub Desktop.
openssl vs digestif
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
external openssl_sha1 : string -> string = "camlopenssl_sha1" | |
external openssl_blake2b : string -> string = "camlopenssl_blake2b" | |
let bench_sha1 () = | |
let input_len = 100_000 in | |
let input = String.make input_len ' ' in | |
let run_digestif () = Digestif.SHA1.digest_string input in | |
let run_openssl () = openssl_sha1 input |> Digestif.SHA1.of_raw_string in | |
Benchmark.throughputN 1 | |
[ ("digestif", run_digestif, ()); ("openssl", run_openssl, ()) ] | |
let bench_blake2b () = | |
let input_len = 100_000 in | |
let input = String.make input_len ' ' in | |
let run_digestif () = Digestif.BLAKE2B.digest_string input in | |
let run_openssl () = | |
openssl_blake2b input |> Digestif.BLAKE2B.of_raw_string | |
in | |
Benchmark.throughputN 1 | |
[ ("digestif", run_digestif, ()); ("openssl", run_openssl, ()) ] | |
let () = | |
let open Benchmark.Tree in | |
register @@ "Digest" | |
@>>> [ "sha1" @> lazy (bench_sha1 ()); "blake2b" @> lazy (bench_blake2b ()) ] | |
let () = Benchmark.Tree.run_global () |
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
(executable | |
(name bench) | |
(libraries benchmark digestif) | |
(foreign_stubs | |
(language c) | |
(names stubs)) | |
(link_flags :standard -cclib -lcrypto)) | |
(rule | |
(alias runtest) | |
(action | |
(run ./bench.exe --all))) |
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
#include <caml/memory.h> | |
#include <caml/alloc.h> | |
#include <openssl/sha.h> | |
#include <openssl/evp.h> | |
value camlopenssl_sha1 (value v_input) | |
{ | |
CAMLparam1(v_input); | |
const char* input = String_val(v_input); | |
int len = caml_string_length(v_input); | |
CAMLlocal1(v_output); | |
v_output = caml_alloc_string(SHA_DIGEST_LENGTH); | |
char* output = Bytes_val(v_output); | |
SHA1(input, len, output); | |
CAMLreturn(v_output); | |
} | |
value camlopenssl_blake2b (value v_input) | |
{ | |
CAMLparam1(v_input); | |
const char* input = String_val(v_input); | |
int len = caml_string_length(v_input); | |
CAMLlocal1(v_output); | |
const EVP_MD *md = EVP_blake2b512(); | |
int output_size = EVP_MD_size(md); | |
v_output = caml_alloc_string(output_size); | |
char* output = Bytes_val(v_output); | |
EVP_Digest(input, len, output, &output_size, md, NULL); | |
CAMLreturn(v_output); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment