Cryptohash provides OCaml bindings to various cryptographic hash functions.
The following functions are supported:
- SHA-1
- SHA-2 (SHA-224, SHA-256, SHA-384, SHA-512)
- SHA-3 (SHA3-224, SHA3-256, SHA3-384, SHA3-512)
- Whirlpool
- Ripemd-160 and Ripemd-128
- MD2, MD4, and MD5
- Tiger and Tiger2
The hash algorithms are implemented in C for performance reasons. Currently, the implementation provided by the project saphir is used (sphlib-3, slightly modified for the latest change in the sha-3 draft)
Both sphlib and the bindings are distributed under a MIT style license. Consult the LICENSE* files in the root folder for details.
The bindings follow the interface of the Digest module included in the OCaml standard library and ocaml-sha.
Usually, the bindings are independent of each other. There are
separate libraries and findlib packages for each hash function. You
need the findlib package cryptohash.sha512 for sha-512,
cryptohash.sha3-224 for sha3-224 and so forth.
- findlib > 1.5 (for building and possibly the bytes module)
- oUnit >= 2 (build only)
- omake (build only)
- ocaml >= 4
If you checkout from git, you first need to create the configure script:
cd src
autoconfThen run omake from the root folder:
omake allYou can disable or enable native code generation with:
omake all NATIVE_ENABLED=false
omake all NATIVE_ENABLED=trueThe default depend on your omake configuration.
omake docA small selection of test cases are run during the build process.
All test cases can be run with the test target:
omake testomake test creates and run a test program that compares the result
of its own computations with public test vectors.
Another possibility is it to specify the target exttest:
omake exttestThe tool generated by exttest searches for external commands in your
PATH that can be used to compute hash digests (openssl, gpg, md5sum ,
sha256sum, jacksum , rhash, sha1sum, ...). Then it generates random
data, compute its checksums and tests if the external tools report the
same checksum.
omake installDocumentation can be found in the mli files and the html files generated by ocamldoc.
Cryptohash was intend as replacement for ocaml-sha. There are however small differences:
- Some execeptions have been changed. The bindings now always follow the convention of
Digest.Invalid_argis thrown, if you don't designate a valid substring / subbuffer;Sys_erroris thrown in case of an i/o error. finalizeresets the context.- the type
bufwas changed from
type buf = (int,Bigarray.int8_unsigned_elt,Bigarray.c_layout) Bigarray.Array1.tto
type buf = (char,Bigarray.int8_unsigned_elt,Bigarray.c_layout) Bigarray.Array1.tzerohas been removed.inputandoutputnow have the same semantic asDigest.inputandDigest.output
If your code doesn't depend on these details, you can use the findlib
package cryptohash.legacy as drop in replacement for sha.