Skip to content

Commit

Permalink
Fix mirage-crypto
Browse files Browse the repository at this point in the history
  • Loading branch information
gabrielmoise17 committed Oct 11, 2024
1 parent ea2403d commit 8460a03
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 15 deletions.
2 changes: 1 addition & 1 deletion cohttp-async.opam
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ depends: [
"cohttp" {= version}
"conduit-async" {>= "1.2.0"}
"magic-mime"
"mirage-crypto" {with-test}
"digestif" {with-test}
"logs"
"fmt" {>= "0.8.2"}
"sexplib0"
Expand Down
4 changes: 2 additions & 2 deletions examples/async/dune
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
(executables
(names hello_world receive_post s3_cp)
(libraries mirage-crypto cohttp-async base async_kernel
core_unix.command_unix cstruct))
(libraries digestif.c cohttp-async base async_kernel core_unix.command_unix
cstruct))

(alias
(name runtest)
Expand Down
17 changes: 5 additions & 12 deletions examples/async/s3_cp.ml
Original file line number Diff line number Diff line change
Expand Up @@ -169,8 +169,7 @@ module Auth = struct

let digest s =
(* string -> sha256 as a hex string *)
Mirage_crypto.Hash.(digest `SHA256 (Cstruct.of_string s))
|> Compat.cstruct_to_hex_string
Digestif.SHA256.(digest_string s |> to_hex)

let make_amz_headers ?body time =
(* Return x-amz-date and x-amz-sha256 headers *)
Expand Down Expand Up @@ -238,16 +237,12 @@ module Auth = struct
Printf.sprintf "AWS4-HMAC-SHA256\n%s\n%s\n%s" time_str scope_str hashed_req

let make_signing_key ?date ~region ~service ~secret_access_key () =
let mac k v =
Mirage_crypto.Hash.(mac `SHA256 ~key:k (Cstruct.of_string v))
in
let mac k v = Digestif.SHA256.(hmac_string ~key:k v |> to_raw_string) in
let date' =
match date with None -> Date.today ~zone:Time.Zone.utc | Some d -> d
in
let date_str = Date.to_string_iso8601_basic date' in
let date_key =
mac (Cstruct.of_string ("AWS4" ^ secret_access_key)) date_str
in
let date_key = mac ("AWS4" ^ secret_access_key) date_str in
let date_region_key = mac date_key (string_of_region region) in
let date_region_service_key =
mac date_region_key (string_of_service service)
Expand Down Expand Up @@ -277,14 +272,12 @@ module Auth = struct
(string_of_service service)
in
let signature =
Mirage_crypto.Hash.(
mac `SHA256 ~key:signing_key (Cstruct.of_string string_to_sign))
Digestif.SHA256.(hmac_string ~key:signing_key string_to_sign |> to_hex)
in
let auth_header =
Printf.sprintf
"AWS4-HMAC-SHA256 Credential=%s,SignedHeaders=%s,Signature=%s" creds
signed_headers
(Compat.cstruct_to_hex_string signature)
signed_headers signature
in
[ ("Authorization", auth_header) ]
end
Expand Down

0 comments on commit 8460a03

Please sign in to comment.