12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364(*
* Copyright (c) 2017 Christiano F. Haesbaert <haesbaert@haesbaert.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.
*)openDigestiftypet=|Plaintext|Sha1|Sha2_256|Sha2_512typekey={hmac:t;(* Hmac algorithm *)key:string;(* The actual hmac key *)}letto_string=function|Plaintext->"none"|Sha1->"hmac-sha1"|Sha2_256->"hmac-sha2-256"|Sha2_512->"hmac-sha2-512"letof_string=function|"none"->OkPlaintext|"hmac-sha1"->OkSha1|"hmac-sha2-256"->OkSha2_256|"hmac-sha2-512"->OkSha2_512|s->Error("Unknown mac "^s)letdigest_len=function|Plaintext->0|Sha1->SHA1.digest_size|Sha2_256->SHA256.digest_size|Sha2_512->SHA512.digest_sizeletkey_len=function|Plaintext->0|Sha1->SHA1.digest_size|Sha2_256->SHA256.digest_size|Sha2_512->SHA512.digest_sizeletknowns=Result.is_ok(of_strings)letsupported=[Sha2_256;Sha2_512;Sha1]lethmacvhmac~keydata=matchhmacwith|Plaintext->""|Sha1->SHA1.(hmaci_string~key(funf->List.iterfdata)|>to_raw_string)|Sha2_256->SHA256.(hmaci_string~key(funf->List.iterfdata)|>to_raw_string)|Sha2_512->SHA512.(hmaci_string~key(funf->List.iterfdata)|>to_raw_string)