Module Mhash
Modular Hashing interface.
This library exposes a hasher
type that can be used to describe how to hash values of a given type, regardless of the concrete hash function used.
It is inspired by Rust's Hash trait.
A concrete hash function can be described using hash_algo
, which is similar to Rust's Hasher trait.
See Mhash_fnv
or Mhash_sha
for concrete functions.
type ('ctx, 'output) hash_algo
=
{
}
A hash algorithm, like murmur3, Sha1, FNV, etc.
- parameter 'ctx
the internal mutable context that contains the intermediate hashing state.
- parameter 'output
the result of hashing a value. It can be an integer but might be larger (e.g. 512 bits for SHA512).
type 'a hasher
=
{
hash_into : ctx out. ('ctx, 'out) hash_algo -> 'ctx -> 'a -> unit;
}
A hash function for a type
'a
.It updates the given
ctx
using thehash_algo
core functions and possibly otherhasher
values. This works for any concrete hash function that implementshash_algo
.
val bytes : bytes hasher
val string : string hasher
val int : int hasher
val nativeint : nativeint hasher
val int32 : int32 hasher
val int64 : int64 hasher
val bool : bool hasher
val char : char hasher
val trivial : 'a hasher
Trivial hasher, does nothing.
val option : 'a hasher -> 'a option hasher
Option hasher. It calls
h
onx
if the input isSome x
, and also injects the constructor into the hash context.
val map : f:('a -> 'b) -> 'b hasher -> 'a hasher
map ~f h
applies hasherh
tof x
in order to hashx
.
val iter : 'a hasher -> (('a -> unit) -> unit) hasher
Hash an iterator by applying the hasher to each element.
val seq : 'a hasher -> 'a Stdlib.Seq.t hasher
Hash a sequence of items by applying the hasher to each element.
val pair : 'a hasher -> 'b hasher -> ('a * 'b) hasher
val tup2 : 'a hasher -> 'b hasher -> ('a * 'b) hasher
val tup3 : 'a hasher -> 'b hasher -> 'c hasher -> ('a * 'b * 'c) hasher
val tup4 : 'a hasher -> 'b hasher -> 'c hasher -> 'd hasher -> ('a * 'b * 'c * 'd) hasher
val hash : algo:('a, 'output) hash_algo -> hash:'b hasher -> 'b -> 'output
Hash a value using the given algorithm and type hasher.