use scanf
This commit is contained in:
parent
805af58fe6
commit
3195ff5c81
3 changed files with 39 additions and 58 deletions
|
|
@ -84,9 +84,8 @@ let pp =
|
||||||
in
|
in
|
||||||
fun ppf { sign; currency; value; fraction } ->
|
fun ppf { sign; currency; value; fraction } ->
|
||||||
(* TODO
|
(* TODO
|
||||||
depends on the currency's number of fraction digits
|
depends on the currency's number of fraction digits *)
|
||||||
assumes value and fraction are in bounds *)
|
pf ppf "%a%s:%Lu.%02lu" (Fmt.option pp_sign) sign currency value fraction
|
||||||
pf ppf "%a%s:%Ld.%02ld" (Fmt.option pp_sign) sign currency value fraction
|
|
||||||
|
|
||||||
let to_string = Fmt.str "%a" pp
|
let to_string = Fmt.str "%a" pp
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -21,34 +21,25 @@ type t = {
|
||||||
ht: (EddsaPublicKey.t, key) Hashtbl.t;
|
ht: (EddsaPublicKey.t, key) Hashtbl.t;
|
||||||
}
|
}
|
||||||
|
|
||||||
(* -- util -- *)
|
let parse_filename =
|
||||||
|
let scan_filename s =
|
||||||
|
Scanf.sscanf_opt s "%Lu-%Lu" (fun t1 t2 ->
|
||||||
|
(Absolute.of_s t1, Absolute.of_s t2))
|
||||||
|
in
|
||||||
|
fun fpath -> scan_filename (Fpath.filename fpath)
|
||||||
|
|
||||||
(* TODO time *)
|
let pp_filename =
|
||||||
let time_abs_of_string s =
|
let to_int64 abs =
|
||||||
int_of_string_opt s |> Option.map (fun n -> Absolute.of_s (Int64.of_int n))
|
abs |> Timestamp.of_absolute |> Timestamp.to_s |> function
|
||||||
|
| None ->
|
||||||
let t1_t2_of_fpath fpath =
|
(* (= `never`) this should not happen given resonable config value *)
|
||||||
let fname = Fpath.filename fpath in
|
Fmt.failwith "encountered timestamp with value `never`"
|
||||||
match String.split_on_char '-' fname with
|
| Some i -> i
|
||||||
| [] -> Fmt.failwith "not possible"
|
in
|
||||||
| [ t1; t2 ] -> (
|
fun ppf (t1, t2) -> Fmt.pf ppf "%Lu-%Lu" (to_int64 t1) (to_int64 t2)
|
||||||
match (time_abs_of_string t1, time_abs_of_string t2) with
|
|
||||||
| None, _ | _, None -> None
|
|
||||||
| Some t1, Some t2 -> Some (t1, t2))
|
|
||||||
| _ -> None
|
|
||||||
|
|
||||||
let time_abs_to_string abs =
|
|
||||||
abs
|
|
||||||
|> Timestamp.of_absolute
|
|
||||||
|> Timestamp.to_s
|
|
||||||
|> Option.get
|
|
||||||
|> Int64.to_int
|
|
||||||
|> string_of_int
|
|
||||||
|
|
||||||
let key_fpath k =
|
let key_fpath k =
|
||||||
let t1 = time_abs_to_string k.t1 in
|
let fname = Fmt.str "%a" pp_filename (k.t1, k.t2) in
|
||||||
let t2 = time_abs_to_string k.t2 in
|
|
||||||
let fname = Fmt.str "%s-%s" t1 t2 in
|
|
||||||
Fpath.(v Cfg.key_dir / fname)
|
Fpath.(v Cfg.key_dir / fname)
|
||||||
|
|
||||||
(* -- IO -- *)
|
(* -- IO -- *)
|
||||||
|
|
@ -86,8 +77,7 @@ let get_key_dir_contents dir =
|
||||||
let gen_key t1 t2 =
|
let gen_key t1 t2 =
|
||||||
let priv, pub = EddsaPrivateKey.generate () in
|
let priv, pub = EddsaPrivateKey.generate () in
|
||||||
Log.debug (fun m ->
|
Log.debug (fun m ->
|
||||||
m "generated key (%s-%s):@,`%s`" (time_abs_to_string t1)
|
m "generated key (%a):@,`%s`" pp_filename (t1, t2)
|
||||||
(time_abs_to_string t2)
|
|
||||||
(EddsaPublicKey.to_b32 pub));
|
(EddsaPublicKey.to_b32 pub));
|
||||||
{ priv; pub; t1; t2 }
|
{ priv; pub; t1; t2 }
|
||||||
|
|
||||||
|
|
@ -135,7 +125,7 @@ let load_key fpath =
|
||||||
match Fpath.equal (Fpath.normalize fpath) sm_key_fpath with
|
match Fpath.equal (Fpath.normalize fpath) sm_key_fpath with
|
||||||
| true -> Ok None
|
| true -> Ok None
|
||||||
| false -> (
|
| false -> (
|
||||||
match t1_t2_of_fpath fpath with
|
match parse_filename fpath with
|
||||||
| None -> Fmt.error "invalid file `%a`" Fpath.pp fpath
|
| None -> Fmt.error "invalid file `%a`" Fpath.pp fpath
|
||||||
| Some (t1, t2) ->
|
| Some (t1, t2) ->
|
||||||
let* priv = read_key fpath in
|
let* priv = read_key fpath in
|
||||||
|
|
|
||||||
|
|
@ -55,33 +55,25 @@ type t = {
|
||||||
ht: (RsaPublicKey.t, key) Hashtbl.t;
|
ht: (RsaPublicKey.t, key) Hashtbl.t;
|
||||||
}
|
}
|
||||||
|
|
||||||
(* -- util -- *)
|
let parse_filename =
|
||||||
|
let scan_filename s =
|
||||||
|
Scanf.sscanf_opt s "%Lu-%Lu" (fun t1 t2 ->
|
||||||
|
(Absolute.of_s t1, Absolute.of_s t2))
|
||||||
|
in
|
||||||
|
fun fpath -> scan_filename (Fpath.filename fpath)
|
||||||
|
|
||||||
let time_abs_of_string s =
|
let pp_filename =
|
||||||
int_of_string_opt s |> Option.map (fun n -> Absolute.of_s (Int64.of_int n))
|
let to_int64 abs =
|
||||||
|
abs |> Timestamp.of_absolute |> Timestamp.to_s |> function
|
||||||
let t1_t2_of_fpath fpath =
|
| None ->
|
||||||
let fname = Fpath.filename fpath in
|
(* (= `never`) this should not happen given resonable config value *)
|
||||||
match String.split_on_char '-' fname with
|
Fmt.failwith "encountered timestamp with value `never`"
|
||||||
| [] -> Fmt.failwith "not possible"
|
| Some i -> i
|
||||||
| [ t1; t2 ] -> (
|
in
|
||||||
match (time_abs_of_string t1, time_abs_of_string t2) with
|
fun ppf (t1, t2) -> Fmt.pf ppf "%Lu-%Lu" (to_int64 t1) (to_int64 t2)
|
||||||
| None, _ | _, None -> None
|
|
||||||
| Some t1, Some t2 -> Some (t1, t2))
|
|
||||||
| _ -> None
|
|
||||||
|
|
||||||
let time_abs_to_string abs =
|
|
||||||
abs
|
|
||||||
|> Timestamp.of_absolute
|
|
||||||
|> Timestamp.to_s
|
|
||||||
|> Option.get
|
|
||||||
|> Int64.to_int
|
|
||||||
|> string_of_int
|
|
||||||
|
|
||||||
let key_fpath k =
|
let key_fpath k =
|
||||||
let t1 = time_abs_to_string k.t1 in
|
let fname = Fmt.str "%a" pp_filename (k.t1, k.t2) in
|
||||||
let t2 = time_abs_to_string k.t2 in
|
|
||||||
let fname = Fmt.str "%s-%s" t1 t2 in
|
|
||||||
Fpath.(v Cfg.key_dir / k.section_name / fname)
|
Fpath.(v Cfg.key_dir / k.section_name / fname)
|
||||||
|
|
||||||
(* -- IO -- *)
|
(* -- IO -- *)
|
||||||
|
|
@ -128,8 +120,8 @@ let gen_key ~section_name t1 t2 =
|
||||||
let bits = Cfg.rsa_keysize ~section_name in
|
let bits = Cfg.rsa_keysize ~section_name in
|
||||||
let priv, pub = RsaPrivateKey.generate ~bits () in
|
let priv, pub = RsaPrivateKey.generate ~bits () in
|
||||||
Log.debug (fun m ->
|
Log.debug (fun m ->
|
||||||
m "generated key (%s-%s):@,`%s`" (time_abs_to_string t1)
|
m "generated key (%a):@,`%s`" pp_filename (t1, t2)
|
||||||
(time_abs_to_string t2) (RsaPublicKey.to_b32 pub));
|
(RsaPublicKey.to_b32 pub));
|
||||||
{ section_name; priv; pub; t1; t2 }
|
{ section_name; priv; pub; t1; t2 }
|
||||||
|
|
||||||
let sort_keys l = List.sort (fun a b -> Absolute.compare a.t2 b.t2) l
|
let sort_keys l = List.sort (fun a b -> Absolute.compare a.t2 b.t2) l
|
||||||
|
|
@ -178,7 +170,7 @@ let load_key ~section_name fpath =
|
||||||
match Fpath.equal (Fpath.normalize fpath) sm_key_fpath with
|
match Fpath.equal (Fpath.normalize fpath) sm_key_fpath with
|
||||||
| true -> Ok None
|
| true -> Ok None
|
||||||
| false -> (
|
| false -> (
|
||||||
match t1_t2_of_fpath fpath with
|
match parse_filename fpath with
|
||||||
| None -> Fmt.error "invalid file `%a`" Fpath.pp fpath
|
| None -> Fmt.error "invalid file `%a`" Fpath.pp fpath
|
||||||
| Some (t1, t2) ->
|
| Some (t1, t2) ->
|
||||||
let* priv = read_rsa fpath in
|
let* priv = read_rsa fpath in
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue