implement hash

TODO good handling of 0-terminated C-string
This commit is contained in:
swrup 2025-10-17 22:04:35 +02:00
parent 6220bbdae1
commit 0a24b41d70
5 changed files with 45 additions and 15 deletions

View file

@ -26,6 +26,15 @@
- outdated doc(?)
- some missing struct documentation *)
(* TODO hash and C(ancer)-terminated strings
- "A JSON object is canonicalized by converting it to an ASCII byte array
with the algorithm specified in RFC 8785. The resulting bytes are
terminated with a single 0-byte and then hashed with SHA512."
- from the code it looks like its the same for all stringy-strings
!! that are not raw bytes data
*)
module UTIL = struct
let int32_size = 4
let int64_size = 8
@ -71,9 +80,21 @@ module UTIL = struct
record (fun v -> { v }) |+ field (bytes 64) (fun t -> t.v) |> sealr
end
module MK_HASH_32 () = struct
module MK_HASH_32 () : sig
type t = private { hash: string }
val of_string : string -> t
val bin : t Bin.t
end = struct
type t = { hash: string }
let of_string s =
match String.length s = 32 with
| false -> Fmt.failwith "SHA256 failure: data is not 32 bytes"
| true ->
let hash = Digestif.SHA256.(to_raw_string (digest_string s)) in
{ hash }
let bin =
let open Bin in
record (fun hash -> { hash })
@ -81,9 +102,21 @@ module UTIL = struct
|> sealr
end
module MK_HASH_64 () = struct
module MK_HASH_64 () : sig
type t = private { hash: string }
val f : string -> t
val bin : t Bin.t
end = struct
type t = { hash: string }
let f s =
match String.length s = 64 with
| false -> Fmt.failwith "SHA512 failure: data is not 64bytes"
| true ->
let hash = Digestif.SHA512.(to_raw_string (digest_string s)) in
{ hash }
let bin =
let open Bin in
record (fun hash -> { hash })
@ -183,7 +216,7 @@ module EncryptedLinkSecretP = MK_64 ()
module DenominationBlindingKeyP = MK_32 ()
(* GNUNET_CRYPTO format *)
module RsaPublicKey = struct
module GNUNET_RsaPublicKey = struct
(* libgnuutil format:
https://docs.gnunet.org/doxygen/d9/dbe/structGNUNET__CRYPTO__RsaPublicKeyHeaderP.html
https://docs.gnunet.org/doxygen/d6/d70/group__libgnunetutil.html#ga9c99a81e8cd649c1c925d23211a0738f