package digestif
Install
Dune Dependency
Authors
Maintainers
Sources
sha256=858fcaaf983c528f8e744f81accc7b0dd254c96b17222f12e1f270e8521d3799
sha512=019dfc8dd4a5e5f01fd142729d09135aca3d77a463c2910449203661f7e808ded68c9078b2cc1eb0f7acac7dcf21fa6250a93d8976ac94cd2cfd44b82eac8242
Description
Digestif is a toolbox to provide hashes implementations in C and OCaml.
It uses the linking trick and user can decide at the end to use the C implementation or the OCaml implementation.
We provides implementation of:
- MD5
- SHA1
- SHA224
- SHA256
- SHA384
- SHA512
- SHA3
- Keccak-256
- WHIRLPOOL
- BLAKE2B
- BLAKE2S
- RIPEMD160
Published: 28 Mar 2022
README
Digestif - Hash algorithms in C and OCaml
Digestif is a toolbox which implements hashes:
MD5
SHA1
SHA2
SHA3
WHIRLPOOL
BLAKE2B
BLAKE2S
RIPEMD160
Digestif uses a trick about linking and let the end-user to choose which implementation he wants to use. We provide 2 implementations:
C implementation with
digestif.c
OCaml implementation with
digestif.ocaml
Both are well-tested. However, OCaml implementation is slower than the C implementation.
Note: The linking trick requires digestif.c
or digestif.ocaml
to be the first of your dependencies.
Documentation: https://mirage.github.io/digestif/
Contact: Romain Calascibetta <romain.calascibet ta@gmail.com>
Install & Usage
The library is available on [OPAM][]. You can install it via:
$ opem install digestif
This is a simple program which implements sha1sum
:
$ cat >sha1sum.ml <<EOF
let sum ic =
let tmp = Bytes.create 0x1000 in
let rec go ctx = match input ic tmp 0 0x1000 with
| 0 -> Digestif.SHA1.get ctx
| len ->
let ctx = Digestif.SHA1.feed_bytes ctx ~off:0 ~len tmp in
go ctx
| exception End_of_file -> Digestif.SHA1.get ctx in
go Digestif.SHA1.empty
let () = match Sys.argv with
| [| _; filename; |] when Sys.file_exists filename ->
let ic = open_in filename in
let hash = sum ic in
close_in ic ; print_endline (Digestif.SHA1.to_hex hash)
| [| _ |] ->
let hash = sum stdin in
print_endline (Digestif.SHA1.to_hex hash)
| _ -> Format.eprintf "%s [<filename>]\n%!" Sys.argv.(0)
EOF
$ cat >dune <<EOF
(executable
(name sha1sum)
(libraries digestif))
EOF
$ dune exec ./sha1sum.exe -- sha1sum.ml
fe6e6639a817c23857b507e2d833ec776f23f327
API
For each hash, we implement the same API which is referentially transparent. Then, on the top of these, we reflect functions (like digesti
or hmaci
) with GADT - however, conversion from GADT to hash type is not possible (but you can destruct GADT with to_raw_string
).
Equal/Compare function
We deciced to protect users to timing-attack. In this case, Digestif.equal
(by eqaf package) compares hashes in constant-time.
However, we provide unsafe_compare
function too which is not a constant time function. In some contexts, like ocaml-git
, we don't care about timing attack and we use unsafe_compare
- then, we need to make a wrap where we rename unsafe_compare
to compare
to be able to use it in some functors like Map.Make
or Set.Make
.
It's little annoying to do that but it forces the user to get the right question about security issues. So, please, don't ask to rename this function.
MirageOS
Of course, this package is available to be used on MirageOS (both implementations). User is able to compile digestif.ocaml
with js_of_ocaml
and this package is platform agnostic.
Build Requirements
OCaml >= 4.03.0 (may be less but need test)
base-bytes
meta-packagebase-bigarray
meta-packagedune
to build the project
If you want to compile the test program, you need:
alcotest
Credits
This work is from the nocrypto library and the Vincent hanquez's work in ocaml-sha.
All credits appear in the begin of files and this library is motivated by two reasons:
delete the dependancy with
nocrypto
if you don't use the encryption (and common) partaggregate all hashes functions in one library
Dependencies (7)
- stdlib-shims
- bigarray-compat
- base-bytes
- eqaf
-
conf-pkg-config
build
-
dune
>= "2.6.0"
-
ocaml
>= "4.05.0"
Dev Dependencies (7)
Used by (51)
-
archetype
< "1.2.2"
-
aws
>= "1.2"
-
aws-s3
>= "4.2.0"
-
carton
< "0.4.4"
-
carton-lwt
< "0.4.4"
- chamelon
- dkim
-
dkim-mirage
>= "0.2.0"
-
docfd
>= "2.1.0"
- docteur
- docteur-solo5
- docteur-unix
-
dream
< "1.0.0~alpha3" | >= "1.0.0~alpha7"
- dream-httpaf
-
esperanto-cosmopolitan
>= "0.0.5"
-
git
>= "3.0.0" & < "3.9.1"
-
git-unix
>= "3.0.0" & < "3.9.1"
-
graphql-cohttp
>= "0.9.0"
-
index-bench
>= "1.5.0"
-
irmin
>= "2.0.0"
- irmin-cli
-
irmin-git
>= "2.0.0"
-
irmin-http
= "2.0.0" | >= "2.3.0"
- irmin-tezos
-
irmin-unix
>= "2.0.0"
-
jupyter-kernel
>= "0.4"
-
jwto
>= "0.4.0"
-
learn-ocaml
>= "0.12"
- learn-ocaml-client
- miou
- multihash-digestif
-
naboris
>= "0.1.3"
- nocoiner
-
noise
>= "0.2.0"
- ocluster
- ocluster-worker
- octez-internal-libs
-
ometrics
>= "0.1.2"
- openstellina
-
owi
>= "0.2"
-
soupault
>= "4.4.0"
- spoke
- swhid
- swhid_compute
- tezos-base58
-
tezos-context
< "11.0" | >= "12.0" & < "16.0"
- tezos-context-hash
- tezos-storage
-
tezos-store
< "12.0"
-
webauthn
>= "0.2.0"
- zeit
Conflicts (2)
-
ocaml-freestanding
< "0.6.0"
-
mirage-xen
< "6.0.0"