-
Notifications
You must be signed in to change notification settings - Fork 0
/
hex_encode.ml
85 lines (77 loc) · 2.7 KB
/
hex_encode.ml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
(**************************************************************************)
(* *)
(* Copyright (c) 2014 - 2016. *)
(* Dynamic Ledger Solutions, Inc. <contact@tezos.com> *)
(* *)
(* All rights reserved. No warranty, explicit or implicit, provided. *)
(* *)
(**************************************************************************)
(* Tezos Utility library - Hexadecimal encoding *)
(* From OCaml's stdlib. See [Digest.to_hex] *)
let gen_encode length get s =
let n = length s in
let result = Bytes.create (n*2) in
for i = 0 to n-1 do
Bytes.blit_string (Printf.sprintf "%02x" (get s i)) 0 result (2*i) 2;
done;
Bytes.unsafe_to_string result
let hex_of_bytes = gen_encode MBytes.length MBytes.get_uint8
let hex_encode = gen_encode String.length (fun s i -> int_of_char s.[i])
(* From OCaml's stdlib. See [Digest.from_hex]. *)
let gen_decode create set h =
let n = String.length h in
if n mod 2 <> 0 then invalid_arg ("hex_decode: " ^ h);
let digit c =
match c with
| '0'..'9' -> int_of_char c - int_of_char '0'
| 'A'..'F' -> int_of_char c - int_of_char 'A' + 10
| 'a'..'f' -> int_of_char c - int_of_char 'a' + 10
| _c -> invalid_arg ("hex_decode: " ^ h)
in
let byte i = digit h.[i] lsl 4 + digit h.[i+1] in
let result = create (n / 2) in
for i = 0 to n/2 - 1 do
set result i (byte (2 * i));
done;
result
let hex_decode s =
gen_decode Bytes.create (fun s i c -> Bytes.set s i (char_of_int c)) s |>
Bytes.unsafe_to_string
let bytes_of_hex s =
gen_decode MBytes.create MBytes.set_int8 s
(*
let hex_bytes =
let open Data_encoding in
let schema =
let open Json_schema in
create
{ title = None ;
description = None ;
default = None;
enum = None;
kind = String {
pattern = Some "^[a-zA-Z0-9]+$";
min_length = 0;
max_length = None;
};
format = None ;
id = None } in
conv ~schema hex_of_bytes (Json.wrap_error bytes_of_hex) string
let sha256 =
let open Data_encoding in
let schema =
let open Json_schema in
create
{ title = None ;
description = None ;
default = None;
enum = None;
kind = String {
pattern = Some "^[a-zA-Z0-9]+$";
min_length = 64;
max_length = Some 64;
};
format = Some "sha256" ;
id = None } in
conv ~schema hex_of_bytes (Json.wrap_error bytes_of_hex) string
*)