experimental hashing with oxcaml
1(** Fast SHA256 hashing library with zero-copy C bindings.
2
3 This library provides OCaml bindings to a C SHA256 implementation
4 using bigarrays for efficient, zero-copy hashing. *)
5
6(** {1 Raw C Bindings} *)
7
8module Raw : sig
9 (** Low-level bindings to the C SHA256 implementation.
10
11 This module provides direct access to the C functions with minimal
12 overhead. All operations work with bigarrays for zero-copy performance. *)
13
14 (** The SHA256 context type. This is an abstract type wrapping the C
15 SHA256_CTX structure. *)
16 type t
17
18 (** [create ()] allocates and initializes a new SHA256 context.
19
20 @return A fresh context ready for hashing. *)
21 val create : unit -> t
22
23 (** [update ctx data] updates the hash state with new data.
24
25 This function processes the input data incrementally. It can be called
26 multiple times to hash data in chunks.
27
28 @param ctx The SHA256 context to update
29 @param data A bigarray containing the data to hash. Uses bigarrays for
30 zero-copy access from the C side. *)
31 val update :
32 t ->
33 (char, Bigarray.int8_unsigned_elt, Bigarray.c_layout) Bigarray.Array1.t ->
34 unit
35
36 (** [update_bytes ctx data] updates the hash state with bytes data.
37
38 This is a convenience function that wraps bytes in a bigarray view.
39
40 @param ctx The SHA256 context to update
41 @param data Bytes to hash *)
42 val update_bytes : t -> bytes -> unit
43
44 (** [update_string ctx data] updates the hash state with string data.
45
46 This is a convenience function for hashing strings.
47
48 @param ctx The SHA256 context to update
49 @param data String to hash *)
50 val update_string : t -> string -> unit
51
52 (** [final ctx] finalizes the hash computation and returns the digest.
53
54 After calling this function, the context should not be used again.
55
56 @param ctx The SHA256 context to finalize
57 @return A 32-byte digest as a bytes value *)
58 val final : t -> bytes
59
60 (** [hash data] is a convenience function that performs a complete hash
61 in one operation: create, update, and final.
62
63 @param data The bigarray data to hash
64 @return A 32-byte digest *)
65 val hash :
66 (char, Bigarray.int8_unsigned_elt, Bigarray.c_layout) Bigarray.Array1.t ->
67 bytes
68
69 (** [hash_bytes data] hashes bytes data in one operation.
70
71 @param data The bytes to hash
72 @return A 32-byte digest *)
73 val hash_bytes : bytes -> bytes
74
75 (** [hash_string data] hashes string data in one operation.
76
77 @param data The string to hash
78 @return A 32-byte digest *)
79 val hash_string : string -> bytes
80end
81
82(** {1 High-Level Interface} *)
83
84(** Re-export the Raw module as the main interface.
85
86 The Raw module provides the most efficient interface using bigarrays.
87 Higher-level abstractions can be added in the future if needed. *)
88
89include module type of Raw