123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263moduletypeS=sigvalpbkdf1:password:string->salt:string->count:int->dk_len:int->stringvalpbkdf2:password:string->salt:string->count:int->dk_len:int32->stringendletcdivxy=(* This is lifted from Nocrypto.Uncommon.(//)
(formerly known as [cdiv]). It is part of the documented, publically
exposed _internal_ utility library not for public consumption, hence
the API break that prompted this copy-pasted function. *)ify<1thenraiseDivision_by_zeroelseifx>0then1+((x-1)/y)else0[@@inline]moduleMake(H:Digestif.S):S=structletpbkdf1~password~salt~count~dk_len=ifString.lengthsalt<>8theninvalid_arg"salt should be 8 bytes"elseifcount<=0theninvalid_arg"count must be a positive integer"elseifdk_len<=0theninvalid_arg"derived key length must be a positive integer"elseifdk_len>H.digest_sizetheninvalid_arg"derived key too long"elseletrecloopt=function0->t|i->loopH.(to_raw_string(digest_stringt))(i-1)inString.sub(loop(password^salt)count)0dk_lenletpbkdf2~password~salt~count~dk_len=ifcount<=0theninvalid_arg"count must be a positive integer"elseifdk_len<=0ltheninvalid_arg"derived key length must be a positive integer"elseleth_len=H.digest_sizeanddk_len=Int32.to_intdk_leninletl=cdivdk_lenh_leninletr=dk_len-(l-1)*h_leninletblocki=letrecfuxor=function|0->xor|j->letu=H.(to_raw_string(hmac_string~key:passwordu))infu(Mirage_crypto.Uncommon.xorxoru)(j-1)inletint_i=Bytes.create4inBytes.set_int32_beint_i0(Int32.of_inti);letu_1=H.hmac_string~key:password(salt^Bytes.unsafe_to_stringint_i)inletu_1=H.to_raw_stringu_1infu_1u_1(count-1)inletrecloopblocks=function|0->blocks|i->loop(blocki::blocks)(i-1)inString.concat""(loop[String.sub(blockl)0r](l-1))endletpbkdf1~hash~password~salt~count~dk_len=letmoduleH=(val(Digestif.module_of_hash'hash))inletmodulePBKDF=Make(H)inPBKDF.pbkdf1~password~salt~count~dk_lenletpbkdf2~prf~password~salt~count~dk_len=letmoduleH=(val(Digestif.module_of_hash'prf))inletmodulePBKDF=Make(H)inPBKDF.pbkdf2~password~salt~count~dk_len