123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219(**************************************************************************)(* *)(* Copyright 2017-2018 OCamlPro *)(* *)(* All rights reserved. This file is distributed under the terms of the *)(* GNU Lesser General Public License version 2.1, with the special *)(* exception on linking described in the file LICENSE. *)(* *)(**************************************************************************)openEzHexmoduleRAW=struct(* Code from BLAKE/BLAKE *)(*
external blake2b : (* out *)string -> (* in *)string -> (* key *)string ->
int = "blake2b_ml"
let blake2b ?(key = "") input =
let hash = String.make 64 '\000' in
let n = blake2b hash input key in
assert (n = 0);
hash
*)externalblake2b_size_of_context:unit->int="blake2b_size_of_context_ml"typeblake2b_ctxexternalblake2b_init:bytes->int->blake2b_ctx="blake2b_init_ml"externalblake2b_init_key:bytes->int->string->blake2b_ctx="blake2b_init_key_ml"externalblake2b_update:blake2b_ctx->string->unit="blake2b_update_ml"externalblake2b_final:blake2b_ctx->bytes->unit="blake2b_final_ml"letblake2b_ctx_size=blake2b_size_of_context()letblake2b_init?key?(size=64)()=letctx=Bytes.makeblake2b_ctx_size'\000'inlethash=Bytes.makesize'\000'inletctx=matchkeywith|None->blake2b_initctxsize|Somekey->blake2b_init_keyctxsizekeyinctx,hashletblake2b_update(ctx,_)input=blake2b_updatectxinputletblake2b_final(ctx,hash)=blake2b_finalctxhash;Bytes.unsafe_to_stringhashletblake2b?keyinput=letctx=blake2b_init?key()inblake2b_updatectxinput;blake2b_finalctx(* Checks from Wikipedia :-) *)let()=lettest_BLAKE2b_512input=String.uppercase_ascii(Hex.encode(blake2binput))inassert(test_BLAKE2b_512""="786A02F742015903C6C6FD852552D272912F4740E15847618A86E217F71F5419\
D25E1031AFEE585313896444934EB04B903A685B1448B755D56F701AFE9BE2CE");assert(test_BLAKE2b_512"The quick brown fox jumps over the lazy dog"="A8ADD4BDDDFD93E4877D2746E62817B116364A1FA7BC148D95090BC7333B3673\
F82401CF7AA2E4CB1ECD90296E3F14CB5413F8ED77BE73045B13914CDCD6A918");()(* Code from PolarSSL *)(*
external sha256 : (* out *)string -> (* in *)string -> (* is224 *) bool -> unit = "sha256_ml"
let sha256 input =
let hash = String.make 32 '\000' in
sha256 hash input false;
hash
*)externalsha256_size_of_context:unit->int="sha256_size_of_context_ml"typesha256_ctxexternalsha256_init:string->sha256_ctx="sha256_init_ml"externalsha256_update:sha256_ctx->string->unit="sha256_update_ml"externalsha256_final:sha256_ctx->string->unit="sha256_final_ml"letsha256_ctx_size=sha256_size_of_context()(* 108 normally *)letsha256_init()=letctx=String.makesha256_ctx_size'\000'inletnew_ctx=sha256_initctxinnew_ctxletsha256_finalctx=lethash=String.make32'\000'insha256_finalctxhash;hashletsha256input=letctx=sha256_init()insha256_updatectxinput;sha256_finalctxlet()=lettest_SHA256input=Hex.encode(sha256input)inassert(test_SHA256""="e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855");assert(test_SHA256"The quick brown fox jumps over the lazy dog"="d7a8fbb307d7809469ca9abcb0082e4f8d5651e46d3cdb762d02d0bf37c9e592");()externalsha3_size_of_context:unit->int="sha3_size_of_context_ml"typesha3_ctxtypesha3_kind=|KEC256|KEC384|KEC512externalsha3_init:string->sha3_kind->sha3_ctx="sha3_init_ml"externalsha3_update:sha3_ctx->string->unit="sha3_update_ml"externalsha3_final:sha3_ctx->string->unit="sha3_final_ml"letsha3_ctx_size=sha3_size_of_context()(* 108 normally *)letsha3_initsha3_kind=letctx=String.makesha3_ctx_size'\000'inletoutput=String.make(matchsha3_kindwith|KEC256->32|KEC384->48|KEC512->64)'\000'inletnew_ctx=sha3_initctxsha3_kindinnew_ctx,outputletsha3_update(ctx,_)s=sha3_updatectxsletsha3_final(ctx,output)=sha3_finalctxoutput;outputletsha3sha3_kindinput=letctx=sha3_initsha3_kindinsha3_updatectxinput;sha3_finalctxlet()=lettest_KECinput=Hex.encode(sha3KEC256input)inassert(test_KEC""="c5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470");assert(test_KEC"The quick brown fox jumps over the lazy dog"="4d741b6f1eb29cb2a9b9911c82f56fa8d73b04959d3d9d222895df6c0b28aa15");()endletdigest=function|`SHA256->RAW.sha256|`SHA3_KEC->RAW.sha3RAW.KEC256|`SHA3_KEC384->RAW.sha3RAW.KEC384|`SHA3_KEC512->RAW.sha3RAW.KEC512moduletypeHASH=sigtypetvalhash:string->t(* typed *)valhash_bytes:bytes->t(* typed *)valraw:t->stringvalsize:intvaldigest:string->string(* untyped *)endmoduleSHA256:HASH=structtypet=stringlethash=RAW.sha256lethash_bytesb=hash(Bytes.unsafe_to_stringb)letdigest=RAW.sha256letrawt=tletsize=32endmoduleSHA3KEC:HASH=structtypet=stringlethash=RAW.sha3RAW.KEC256lethash_bytesb=hash(Bytes.unsafe_to_stringb)letdigest=RAW.sha3RAW.KEC256letrawt=tletsize=32endmoduleSHA3KEC512:HASH=structtypet=stringlethash=RAW.sha3RAW.KEC512lethash_bytesb=hash(Bytes.unsafe_to_stringb)letdigest=RAW.sha3RAW.KEC512letrawt=tletsize=64endmoduleBLAKE2B:HASH=structtypet=stringlethash=RAW.blake2b?key:Nonelethash_bytesb=hash(Bytes.unsafe_to_stringb)letdigest=RAW.blake2b?key:Noneletrawt=tletsize=64end