aboutsummaryrefslogtreecommitdiff
path: root/lib/monocypher.ml
diff options
context:
space:
mode:
Diffstat (limited to 'lib/monocypher.ml')
-rw-r--r--lib/monocypher.ml33
1 files changed, 33 insertions, 0 deletions
diff --git a/lib/monocypher.ml b/lib/monocypher.ml
new file mode 100644
index 0000000..a128f2f
--- /dev/null
+++ b/lib/monocypher.ml
@@ -0,0 +1,33 @@
+(*
+ * SPDX-FileCopyrightText: 2021 petites singularit├ęs <ps-dream@lesoiseaux.io>
+ * SPDX-FileCopyrightText: 2021 pukkamustard <pukkamustard@posteo.net>
+ *
+ * SPDX-License-Identifier: AGPL-3.0-or-later
+ *)
+
+
+open Ctypes
+open Foreign
+
+module Hashing = struct
+ module Blake2b = struct
+
+ let crypto_blake2b_general =
+ foreign "crypto_blake2b_general"
+ (ptr char @-> size_t @-> string_opt @-> size_t @-> string @-> size_t @-> returning void)
+
+ let digest ?key ?(size=64) msg =
+ let hash_ptr = allocate_n char ~count:size in
+ let key_size = Option.(key
+ |> map String.length
+ |> value ~default:0) in
+
+ let () = crypto_blake2b_general
+ hash_ptr (Unsigned.Size_t.of_int size)
+ key (Unsigned.Size_t.of_int key_size)
+ msg (Unsigned.Size_t.of_int (String.length msg))
+ in
+
+ string_from_ptr hash_ptr ~length:size
+ end
+end