Created
January 10, 2014 17:47
-
-
Save zbroyar/8359050 to your computer and use it in GitHub Desktop.
Yet another (simple) OCaml implementation of HMAC-SHA-512.
Require cryptokit and sha opam packages. Cryptokit is needed for just two functions: xor_string and wipe_string.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
open Cryptokit | |
let ($) f x = f x | |
let (%) f g = fun x -> f (g x) | |
let to_hex = transform_string (Hexa.encode()) | |
let of_hex = transform_string (Hexa.decode()) | |
let hmac_sha512 key str = | |
let blocksize = 128 in | |
let hmac_pad key byte = | |
let key = | |
if String.length key > blocksize | |
then Sha512.to_bin % Sha512.string $ key | |
else key | |
in | |
let r = String.make blocksize (Char.chr byte) in | |
xor_string key 0 r 0 (String.length key); r | |
in | |
let ipad = hmac_pad key 0x36 | |
and opad = hmac_pad key 0x5c in | |
let str = Sha512.to_bin % Sha512.string $ (ipad ^ str) in | |
let str = Sha512.to_bin % Sha512.string $ (opad ^ str) in | |
wipe_string ipad; | |
wipe_string opad; | |
str |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment