12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697(*
* Copyright (C) 2006-2009 Vincent Hanquez <tab@snarc.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.
*
* SHA512 OCaml binding
*)typectxtypebuf=(int,Bigarray.int8_unsigned_elt,Bigarray.c_layout)Bigarray.Array1.ttypetexternalinit:unit->ctx="stub_sha512_init"externalunsafe_update_substring:ctx->string->int->int->unit="stub_sha512_update"externalupdate_buffer:ctx->buf->unit="stub_sha512_update_bigarray"externalfinalize:ctx->t="stub_sha512_finalize"externalcopy:ctx->ctx="stub_sha512_copy"externalto_bin:t->string="stub_sha512_to_bin"externalto_hex:t->string="stub_sha512_to_hex"externalof_bin:bytes->t="stub_sha256_of_bin"externalof_hex:string->t="stub_sha256_of_hex"externalfile_fast:string->t="stub_sha512_file"externalequal:t->t->bool="stub_sha512_equal"letblksize=4096letupdate_substringctxsofslen=iflen<=0&&String.lengths<ofs+lentheninvalid_arg"substring";unsafe_update_substringctxsofslenletupdate_stringctxs=unsafe_update_substringctxs0(String.lengths)letstrings=letctx=init()inunsafe_update_substringctxs0(String.lengths);finalizectxletzero=string""letsubstringsofslen=iflen<=0&&String.lengths<ofs+lentheninvalid_arg"substring";letctx=init()inunsafe_update_substringctxsofslen;finalizectxletbufferbuf=letctx=init()inupdate_bufferctxbuf;finalizectxletchannelchanlen=letctx=init()andbuf=Bytes.createblksizeinletleft=reflenandeof=reffalseinwhile(!left==-1||!left>0)&¬!eofdoletlen=if!left<0thenblksizeelse(min!leftblksize)inletreaded=Stdlib.inputchanbuf0leninifreaded=0theneof:=trueelse(letbuf=Bytes.unsafe_to_stringbufinunsafe_update_substringctxbuf0readed;(* [unsafe_update_substring] does not hold on to [buf],
so we can mutate it again now *)if!left<>-1thenleft:=!left-readed)done;if!left>0&&!eofthenraiseEnd_of_file;finalizectxletfilename=letchan=open_in_binnameinletdigest=channelchan(-1)inclose_inchan;digestletinputchan=channelchan(-1)letoutputchandigest=output_stringchan(to_hexdigest)