Bytesrw_crypto.HashGeneric and dedicated message digests (hashes).
Warning. Before using a hash algorithm, check it is Hash.Algorithm.is_supported otherwise panics are raised on usage.
Dedicated modules. If you need to manipulate hash values of a specific algorithm beyond a simple check it's better to use a dedicated module. By giving you a proper type for its hash values it will make it for clearer and avoid possible mixups; For example here is a dedicated module for algorithm Sha_384:
module Sha_384 = Bytesrw_crypto.Hash.Make
(struct let algorithm = Bytesrw_crypto.Hash.Algorithm.Sha_384 end)A few predefined hash modules are provided for hashes in widespread use in 2025.
module Algorithm : sig ... endHash algorithm identifiers.
The type for generic hashes. These hashes are not tied to an algorithm. Values of this type just represent the result of any hash algorithm as a sequence of bytes. Use a dedicated module to work with a particular type hash.
val length : t -> intlength h is the length of hash h. See also Algorithm.length.
module State : sig ... endHash state.
value state is the hash of state.
Warning. This must be called only once. It has an effect on state. Trying to do the following operations afterwards raises Bytesrw_crypto.Panic exceptions:
State.update or State.copy. Make a State.copy before if you want to get an intermediate hash.value or verify_value on state.verify_value state h checks in constant time that value state is equal to h.
Warning. This has all the caveats of value.
val string : Algorithm.t -> string -> tstring alg s is the alg hash of s.
val bytes : Algorithm.t -> bytes -> tbytes alg b is the alg hash of b.
val slice : Algorithm.t -> Bytesrw.Bytes.Slice.t -> tslice alg s is the alg hash of the bytes in the range of s.
val reader : Algorithm.t -> Bytesrw.Bytes.Reader.t -> treader alg r is the alg hash of stream r. This consumes the reader. See also reads.
val reads : State.t -> Bytesrw.Bytes.Reader.t -> Bytesrw.Bytes.Reader.treads state r is hr with:
hr a reader that taps the reads of r to update state.state, the hash state to udpate.To get the final hash result use value on state once.
val writes : State.t -> Bytesrw.Bytes.Writer.t -> Bytesrw.Bytes.Writer.twrites state w is hw with:
hw a writer that taps the writes to update state before giving them to w.state, the hash state to udpate.To get the final hash result use value on state once.
equal h0 h1 uses Verify.equal_strings to assert the equality of h0 and h1. Raises Invalid_argument if the hashes length is not positive and equal.
val to_binary_string : t -> stringto_binary_string h is a binary representation of h of length length.
val of_binary_string : ?length:int -> string -> (t, string) Stdlib.resultof_binary_string s is a hash from binary representation stored in s. If length is specified, errors if the hash is not length bytes.
val to_hex : t -> stringto_hex h is the binary representation of h using lowercase US-ASCII hex digits.
val of_hex : ?length:int -> string -> (t, string) Stdlib.resultof_hex s parses a sequence of hex digits into a hash. If length is specified, errors if the resulting hash has not length bytes.
val pp : Stdlib.Format.formatter -> t -> unitpp formats hashes for inspection.
module type T = sig ... endThe type for dedicated hash modules.
module type ALGORITHM = sig ... endThe type for hash algorithm specification.