1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495(*
* Copyright (c) 2013-2017 Thomas Gazagnaire <thomas@gazagnaire.org>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*)includeHash_intfmoduleMake(H:Digestif.S)=structtypet=H.texternalget_64:string->int->int64="%caml_string_get64u"letshort_hashc=Int64.to_int(get_64(H.to_raw_stringc)0)lethash_size=H.digest_sizeletof_hexs=matchH.consistent_of_hexswith|x->Okx|exceptionInvalid_argumente->Error(`Msge)letpp_hexppfx=Fmt.stringppf(H.to_hexx)lett=Type.map~pp:pp_hex~of_string:of_hexType.(string_of(`Fixedhash_size))H.of_raw_stringH.to_raw_stringlethashs=H.digesti_stringsendmoduleMake_BLAKE2B(D:sigvaldigest_size:intend)=Make(Digestif.Make_BLAKE2B(D))moduleMake_BLAKE2S(D:sigvaldigest_size:intend)=Make(Digestif.Make_BLAKE2S(D))moduleSHA1=Make(Digestif.SHA1)moduleRMD160=Make(Digestif.RMD160)moduleSHA224=Make(Digestif.SHA224)moduleSHA256=Make(Digestif.SHA256)moduleSHA384=Make(Digestif.SHA384)moduleSHA512=Make(Digestif.SHA512)moduleBLAKE2B=Make(Digestif.BLAKE2B)moduleBLAKE2S=Make(Digestif.BLAKE2S)moduleTyped(K:S)(V:Type.S)=structincludeKtypevalue=V.tletpre_hash=Type.unstage(Type.pre_hashV.t)lethashv=K.hash(pre_hashv)endmoduleV1(K:S):Swithtypet=K.t=structtypet=K.tlethash=K.hashletshort_hash=K.short_hashlethash_size=K.hash_sizeleth=Type.string_of`Int64letto_bin_key=Type.unstage(Type.to_bin_stringK.t)letof_bin_key=Type.unstage(Type.of_bin_stringK.t)letsize_of=Type.Size.usingto_bin_key(Type.Size.th)letencode_bin=letencode_bin=Type.unstage(Type.encode_binh)inType.stage(fune->encode_bin(to_bin_keye))letdecode_bin=letdecode_bin=Type.unstage(Type.decode_binh)inType.stage@@funbufoff->letn,v=decode_binbufoffin(n,matchof_bin_keyvwith|Okv->v|Error(`Msge)->Fmt.failwith"decode_bin: %s"e)lett=Type.likeK.t~bin:(encode_bin,decode_bin,size_of)end