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