hash: use of_raw_string_opt
This commit is contained in:
parent
64ff6f7eb4
commit
5ccf9f34e7
5 changed files with 43 additions and 30 deletions
20
src/hash.ml
20
src/hash.ml
|
|
@ -1,3 +1,4 @@
|
|||
(* TODO use SHA512.of_raw_string_opt *)
|
||||
open Digestif
|
||||
|
||||
module type S = sig
|
||||
|
|
@ -18,9 +19,9 @@ module H32 = struct
|
|||
let hash s = SHA256.(digest_string s)
|
||||
|
||||
let of_octets s =
|
||||
match String.length s = 32 with
|
||||
| false -> Fmt.failwith "Hash.of_octets failure: data is not 32 bytes"
|
||||
| true -> SHA256.of_raw_string s
|
||||
match SHA256.of_raw_string_opt s with
|
||||
| None -> Fmt.failwith "H32.of_octets failure: data is not 32 bytes"
|
||||
| Some t -> t
|
||||
|
||||
let to_octets = SHA256.to_raw_string
|
||||
let of_b32 s = Result.map of_octets (B32.decode s)
|
||||
|
|
@ -48,16 +49,11 @@ module H64 = struct
|
|||
let hash s = SHA512.(digest_string s)
|
||||
|
||||
let of_octets s =
|
||||
match String.length s = 64 with
|
||||
| false -> Fmt.failwith "Hash.of_octets failure: data is not 64 bytes"
|
||||
| true -> SHA512.of_raw_string s
|
||||
|
||||
let to_octets v =
|
||||
let s = SHA512.to_raw_string v in
|
||||
match String.length s = 64 with
|
||||
| false -> Fmt.failwith "Hash.to_octets failure: data is not 64 bytes"
|
||||
| true -> s
|
||||
match SHA512.of_raw_string_opt s with
|
||||
| None -> Fmt.failwith "H64.of_octets failure: data is not 64 bytes"
|
||||
| Some t -> t
|
||||
|
||||
let to_octets = SHA512.to_raw_string
|
||||
let of_b32 s = Result.map of_octets (B32.decode s)
|
||||
|
||||
let bin =
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue