1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
module S1 = Mirage_crypto.Hash.SHA1
module S2 = Mirage_crypto.Hash.SHA256
module S5 = Mirage_crypto.Hash.SHA512
let of_string = Cstruct.of_string
let to_string = Cstruct.to_string
let hmac_sha1 ~secret payload =
to_string @@ S1.hmac ~key:(of_string secret) @@ of_string payload
let hmac_sha256 ~secret payload =
to_string @@ S2.hmac ~key:(of_string secret) @@ of_string payload
let hmac_sha512 ~secret payload =
to_string @@ S5.hmac ~key:(of_string secret) @@ of_string payload
let no_trace char = char != '-'
let hmac ~hash =
let algorithm =
hash
|> Base.String.lowercase
|> Base.String.to_list
|> Base.List.filter ~f:no_trace
|> Base.String.of_char_list
in
match algorithm with
| "sha1" ->
hmac_sha1
| "sha256" ->
hmac_sha256
| "sha512" ->
hmac_sha512
| _ ->
failwith ("Invalid hash algorithm: " ^ hash)