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 = {
init : unit -> 'ctx;

Create a new single-use context.

finalize : 'ctx -> 'output;

Turn the context into a hash.

bytes : 'ctx -> bytes -> unit;

Hash bytes.

subbytes : 'ctx -> bytes -> int -> int -> unit;

Hash a slice of the bytes.

string : 'ctx -> string -> unit;

Hash a string

substring : 'ctx -> string -> int -> int -> unit;

Hash a slice of the string.

bool : 'ctx -> bool -> unit;

Hash a boolean.

int : 'ctx -> int -> unit;

Hash an integer.

int32 : 'ctx -> int32 -> unit;

Hash an int32.

int64 : 'ctx -> int64 -> unit;

Hash an int64.

nativeint : 'ctx -> nativeint -> unit;

Hash a native integer.

char : 'ctx -> char -> unit;

Hash a single byte.

}

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 the hash_algo core functions and possibly other hasher values. This works for any concrete hash function that implements hash_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 on x if the input is Some x, and also injects the constructor into the hash context.

val map : f:('a -> 'b) -> 'b hasher -> 'a hasher

map ~f h applies hasher h to f x in order to hash x.

val list : 'a hasher -> 'a list hasher

Hash a list by applying the hasher to each element.

val array : 'a hasher -> 'a array hasher

Hash an array by applying the hasher to each element.

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.