aboutsummaryrefslogtreecommitdiff
path: root/lib/monocypher.ml
blob: a128f2f18c057a51fac74d8d71f598595e0aee99 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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