hash: use of_raw_string_opt
This commit is contained in:
parent
2f5489363e
commit
d0cbe128c9
4 changed files with 36 additions and 23 deletions
|
|
@ -29,6 +29,10 @@ let decode s =
|
||||||
| c -> c)
|
| c -> c)
|
||||||
s
|
s
|
||||||
in
|
in
|
||||||
|
(* restore padding for base32 lib *)
|
||||||
|
let n = 8 - (String.length s mod 8) in
|
||||||
|
let pad = String.make n '=' in
|
||||||
|
let s = s ^ pad in
|
||||||
match Base32.decode ~alphabet ~off:0 ~len:(String.length s) s with
|
match Base32.decode ~alphabet ~off:0 ~len:(String.length s) s with
|
||||||
| Error (`Msg e) -> Error e
|
| Error (`Msg e) -> Error e
|
||||||
| Ok v -> Ok v
|
| Ok v -> Ok v
|
||||||
|
|
|
||||||
20
src/hash.ml
20
src/hash.ml
|
|
@ -1,3 +1,4 @@
|
||||||
|
(* TODO use SHA512.of_raw_string_opt *)
|
||||||
open Digestif
|
open Digestif
|
||||||
|
|
||||||
module type S = sig
|
module type S = sig
|
||||||
|
|
@ -18,9 +19,9 @@ module H32 = struct
|
||||||
let hash s = SHA256.(digest_string s)
|
let hash s = SHA256.(digest_string s)
|
||||||
|
|
||||||
let of_octets s =
|
let of_octets s =
|
||||||
match String.length s = 32 with
|
match SHA256.of_raw_string_opt s with
|
||||||
| false -> Fmt.failwith "Hash.of_octets failure: data is not 32 bytes"
|
| None -> Fmt.failwith "H32.of_octets failure: data is not 32 bytes"
|
||||||
| true -> SHA256.of_raw_string s
|
| Some t -> t
|
||||||
|
|
||||||
let to_octets = 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)
|
||||||
|
|
@ -48,16 +49,11 @@ module H64 = struct
|
||||||
let hash s = 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 SHA512.of_raw_string_opt s with
|
||||||
| false -> Fmt.failwith "Hash.of_octets failure: data is not 64 bytes"
|
| None -> Fmt.failwith "H64.of_octets failure: data is not 64 bytes"
|
||||||
| true -> SHA512.of_raw_string s
|
| Some t -> t
|
||||||
|
|
||||||
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
|
|
||||||
|
|
||||||
|
let to_octets = SHA512.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 =
|
||||||
|
|
|
||||||
31
test/b32.ml
31
test/b32.ml
|
|
@ -1,15 +1,28 @@
|
||||||
let () =
|
let round_trip s =
|
||||||
let id s = s |> B32.encode |> B32.decode |> Result.get_ok in
|
let s' = s |> B32.encode |> B32.decode |> Result.get_ok in
|
||||||
let f s =
|
|
||||||
let s' = id s in
|
|
||||||
match s' = s with
|
match s' = s with
|
||||||
| false ->
|
| false -> Fmt.failwith "b32 round trip failure: @\nin :`%S`@\nout:`%S`" s' s
|
||||||
Fmt.failwith "b32 round trip failure: @\nin :`%S`@\nout:`%S`" s' s
|
|
||||||
| true -> ()
|
| true -> ()
|
||||||
in
|
|
||||||
|
let () =
|
||||||
let s = String.init 0xff Char.chr in
|
let s = String.init 0xff Char.chr in
|
||||||
|
round_trip s;
|
||||||
let s_l = List.init 0x0f (fun i -> String.init i Char.chr) in
|
let s_l = List.init 0x0f (fun i -> String.init i Char.chr) in
|
||||||
f s;
|
List.iter round_trip s_l; ()
|
||||||
List.iter f s_l;
|
|
||||||
|
let () =
|
||||||
|
let input = "91JPRV3F5GG4EKJNDSJQ8" in
|
||||||
|
let output =
|
||||||
|
input
|
||||||
|
|> B32.decode
|
||||||
|
|> Result.get_ok
|
||||||
|
|> Hash.H64.hash
|
||||||
|
|> Hash.H64.to_octets
|
||||||
|
|> B32.encode
|
||||||
|
in
|
||||||
|
let expected =
|
||||||
|
"D0R24RZ1TPASVQ2NY56CT8AJDYZE9ZGDB0GVZ05E9D4YGZQW2RC5YFPQ0Q86EPW836DY7VYQTNFFJT3ZR2K508F4JVS5JNJKYN2MMFR"
|
||||||
|
in
|
||||||
|
assert (output = expected);
|
||||||
|
|
||||||
()
|
()
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,7 @@
|
||||||
(test
|
(test
|
||||||
(name b32)
|
(name b32)
|
||||||
(modules b32)
|
(modules b32)
|
||||||
(libraries b32 fmt))
|
(libraries mte b32 fmt))
|
||||||
|
|
||||||
; TODO
|
; TODO
|
||||||
; cram test?
|
; cram test?
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue