This commit is contained in:
swrup 2026-02-05 17:52:18 +01:00
parent e3a0387e30
commit 75512bc21c

View file

@ -6,17 +6,19 @@
(* C-terminated strings (* C-terminated strings
some strings need to be hashed with a '\0' termination char *) some strings need to be hashed with a '\0' termination char *)
module Hash_32 = struct open Digestif
type t = Digestif.SHA256.t
let hash s = Digestif.SHA256.(digest_string s) module Hash_32 = struct
type t = SHA256.t
let hash s = SHA256.(digest_string s)
let of_octets s = let of_octets s =
match String.length s = 32 with match String.length s = 32 with
| false -> Fmt.failwith "Hash.of_octets failure: data is not 32 bytes" | false -> Fmt.failwith "Hash.of_octets failure: data is not 32 bytes"
| true -> Digestif.SHA256.of_raw_string s | true -> SHA256.of_raw_string s
let to_octets = Digestif.SHA256.to_raw_string let to_octets = SHA256.to_raw_string
let of_b32 s = Result.map of_octets (B32.decode s) let of_b32 s = Result.map of_octets (B32.decode s)
let bin = let bin =
@ -37,17 +39,17 @@ module Hash_32 = struct
end end
module Hash_64 = struct module Hash_64 = struct
type t = Digestif.SHA512.t type t = SHA512.t
let hash s = Digestif.SHA512.(digest_string s) let hash s = SHA512.(digest_string s)
let of_octets s = let of_octets s =
match String.length s = 64 with match String.length s = 64 with
| false -> Fmt.failwith "Hash.of_octets failure: data is not 64 bytes" | false -> Fmt.failwith "Hash.of_octets failure: data is not 64 bytes"
| true -> Digestif.SHA512.of_raw_string s | true -> SHA512.of_raw_string s
let to_octets v = let to_octets v =
let s = Digestif.SHA512.to_raw_string v in let s = SHA512.to_raw_string v in
match String.length s = 64 with match String.length s = 64 with
| false -> Fmt.failwith "Hash.to_octets failure: data is not 64 bytes" | false -> Fmt.failwith "Hash.to_octets failure: data is not 64 bytes"
| true -> s | true -> s
@ -77,7 +79,7 @@ module Hash_32_cstr = struct
let hash s = let hash s =
let s = s ^ "\x00" in let s = s ^ "\x00" in
Digestif.SHA256.(digest_string s) SHA256.(digest_string s)
end end
module Hash_64_cstr = struct module Hash_64_cstr = struct
@ -85,7 +87,7 @@ module Hash_64_cstr = struct
let hash s = let hash s =
let s = s ^ "\x00" in let s = s ^ "\x00" in
Digestif.SHA512.(digest_string s) SHA512.(digest_string s)
end end
module type Hash_S = sig module type Hash_S = sig