123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118(*
* Copyright (c) 2013-2022 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"externalswap64:int64->int64="%bswap_int64"letget_64_little_endianstridx=ifSys.big_endianthenswap64(get_64stridx)elseget_64stridxletshort_hashc=Int64.to_int(get_64_little_endian(H.to_raw_stringc)0)letshort_hash_substringbigstring~off=Int64.to_int(Bigstringaf.get_int64_lebigstringoff)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_stringsletto_raw_strings=H.to_raw_stringsletunsafe_of_raw_strings=H.of_raw_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.t[@@derivingirmin~pre_hash]lethashv=K.hash(pre_hash_valuev)endmoduleV1(K:S):Swithtypet=K.t=structtypet=K.t[@@derivingirmin~encode_bin~decode_bin]lethash=K.hashletshort_hash=K.short_hashletshort_hash_substring=K.short_hash_substringlethash_size=K.hash_sizeletint64_to_bin_string=Type.(unstage(to_bin_stringint64))lethash_size_str=int64_to_bin_string(Int64.of_intK.hash_size)letto_raw_string=K.to_raw_stringletunsafe_of_raw_string=K.unsafe_of_raw_stringletencode_binef=fhash_size_str;encode_binefletdecode_binbufpos_ref=pos_ref:=!pos_ref+8;decode_binbufpos_refletsize_of=Type.Size.custom_static(8+hash_size)lett=Type.likeK.t~bin:(encode_bin,decode_bin,size_of)endmoduleSet=structmoduleMake(Hash:S)=structincludeIrmin_data.Fixed_size_string_setletcreate?(initial_slots=0)()=letelt_length=Hash.hash_sizeandhashs=Hash.(short_hash(unsafe_of_raw_strings))andhash_substringt~off~len:_=Hash.short_hash_substringt~offincreate~elt_length~initial_slots~hash~hash_substring()letaddth=addt(Hash.to_raw_stringh)letmemth=memt(Hash.to_raw_stringh)endmoduletypeS=Setend