v1.0
This commit is contained in:
commit
42e5e3321a
12 files changed
+593
No files matched your search
@@ -0,0 +1,4 @@
|
||||
_opam
|
||||
#.*
|
||||
.*~
|
||||
_build
|
||||
Whitespace-only changes.
@@ -0,0 +1,15 @@
|
||||
ISC License
|
||||
|
||||
Copyright (c) 2026 Matthieu Dubuget
|
||||
|
||||
Permission to use, copy, modify, and/or distribute this software for any
|
||||
purpose with or without fee is hereby granted, provided that the above
|
||||
copyright notice and this permission notice appear in all copies.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
|
||||
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
|
||||
FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
|
||||
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
|
||||
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
|
||||
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
||||
PERFORMANCE OF THIS SOFTWARE.
|
||||
@@ -0,0 +1,35 @@
|
||||
(executable
|
||||
(public_name totpin)
|
||||
(name main)
|
||||
(libraries
|
||||
console.tty
|
||||
unix
|
||||
re
|
||||
eio_main
|
||||
console-eio
|
||||
digestif.ocaml
|
||||
totpin_base32))
|
||||
|
||||
(rule
|
||||
(with-stdout-to
|
||||
version.ml
|
||||
(echo "let v = \"%{version:totpin}\"\n")))
|
||||
|
||||
(rule
|
||||
(deps dune)
|
||||
(action
|
||||
(with-stdout-to
|
||||
third-party-licenses.txt
|
||||
(run ../tools/gen_licenses.exe dune))))
|
||||
|
||||
(rule
|
||||
(deps ../LICENSE third-party-licenses.txt)
|
||||
(action
|
||||
(with-stdout-to
|
||||
licenses.ml
|
||||
(progn
|
||||
(echo "let own = {licensetext|")
|
||||
(cat ../LICENSE)
|
||||
(echo "|licensetext}\nlet third_party = {licensetext|")
|
||||
(cat third-party-licenses.txt)
|
||||
(echo "|licensetext}\n")))))
|
||||
+245
@@ -0,0 +1,245 @@
|
||||
open Console
|
||||
open View
|
||||
|
||||
type params = {
|
||||
secret : string;
|
||||
hash : string;
|
||||
digits : int;
|
||||
period : int;
|
||||
issuer : string option;
|
||||
}
|
||||
|
||||
let uri_re = Re.Perl.compile_pat "^otpauth://([A-Za-z]+)/([^?]*)\\?(.*)$"
|
||||
let pair_re = Re.Perl.compile_pat "([^&=]+)=([^&]*)"
|
||||
let pct_re = Re.Perl.compile_pat "%([0-9A-Fa-f][0-9A-Fa-f])"
|
||||
let junk_re = Re.Perl.compile_pat "[ =]"
|
||||
let base32_re = Re.Perl.compile_pat "^[A-Za-z2-7]+$"
|
||||
|
||||
let percent_decode s =
|
||||
Re.replace pct_re s ~f:(fun g ->
|
||||
String.make 1 (Char.chr (int_of_string ("0x" ^ Re.Group.get g 1))))
|
||||
|
||||
let clean_secret s = Re.replace_string junk_re ~by:"" s
|
||||
let ( let* ) = Result.bind
|
||||
|
||||
let check_secret secret =
|
||||
if secret = "" then Error "The secret can’t be void!"
|
||||
else if not (Re.execp base32_re secret) then
|
||||
Error
|
||||
"The secret can only contain the following characters: 'a'..'z', \
|
||||
'A'..'Z' or '2'..'7'!"
|
||||
else Ok secret
|
||||
|
||||
let default =
|
||||
{ secret = ""; hash = "SHA1"; digits = 6; period = 30; issuer = None }
|
||||
|
||||
let parse_int name ~allowed s =
|
||||
match int_of_string_opt s with
|
||||
| Some n when allowed n -> Ok n
|
||||
| _ -> Error (Fmt.str "Invalid %s: %S" name s)
|
||||
|
||||
let parse_uri kind label query =
|
||||
if String.lowercase_ascii kind <> "totp" then
|
||||
Error "Only otpauth://totp/ URIs are supported!"
|
||||
else
|
||||
let params =
|
||||
Re.all pair_re query
|
||||
|> List.map (fun g ->
|
||||
( String.lowercase_ascii (Re.Group.get g 1),
|
||||
percent_decode (Re.Group.get g 2) ))
|
||||
in
|
||||
let get k = List.assoc_opt k params in
|
||||
let* secret =
|
||||
match get "secret" with
|
||||
| Some s -> check_secret (clean_secret s)
|
||||
| None -> Error "No secret in the URI!"
|
||||
in
|
||||
let* hash =
|
||||
match Option.map String.uppercase_ascii (get "algorithm") with
|
||||
| None -> Ok default.hash
|
||||
| Some (("SHA1" | "SHA256" | "SHA512") as h) -> Ok h
|
||||
| Some h -> Error (Fmt.str "Unsupported algorithm: %s" h)
|
||||
in
|
||||
let* digits =
|
||||
match get "digits" with
|
||||
| None -> Ok default.digits
|
||||
| Some d -> parse_int "digits" ~allowed:(fun n -> n = 6 || n = 8) d
|
||||
in
|
||||
let* period =
|
||||
match get "period" with
|
||||
| None -> Ok default.period
|
||||
| Some p -> parse_int "period" ~allowed:(fun n -> n > 0) p
|
||||
in
|
||||
let issuer =
|
||||
match get "issuer" with
|
||||
| Some _ as i -> i
|
||||
| None -> (
|
||||
match String.index_opt (percent_decode label) ':' with
|
||||
| Some i -> Some (String.sub (percent_decode label) 0 i)
|
||||
| None -> None)
|
||||
in
|
||||
Ok { secret; hash; digits; period; issuer }
|
||||
|
||||
let parse input =
|
||||
match Re.exec_opt uri_re input with
|
||||
| Some g -> parse_uri (Re.Group.get g 1) (Re.Group.get g 2) (Re.Group.get g 3)
|
||||
| None ->
|
||||
let* secret = check_secret (clean_secret input) in
|
||||
Ok { default with secret }
|
||||
|
||||
let hmac hash ~key msg =
|
||||
match hash with
|
||||
| "SHA1" -> Digestif.SHA1.(to_raw_string (hmac_string ~key msg))
|
||||
| "SHA256" -> Digestif.SHA256.(to_raw_string (hmac_string ~key msg))
|
||||
| "SHA512" -> Digestif.SHA512.(to_raw_string (hmac_string ~key msg))
|
||||
| h -> invalid_arg ("hmac: " ^ h)
|
||||
|
||||
let rec pow10 = function 0 -> 1 | n -> 10 * pow10 (n - 1)
|
||||
|
||||
let totp p ~drift =
|
||||
let counter = Int64.of_float (Unix.time () /. float_of_int p.period) in
|
||||
let msg = Bytes.create 8 in
|
||||
Bytes.set_int64_be msg 0 (Int64.add counter (Int64.of_int drift));
|
||||
let h =
|
||||
hmac p.hash ~key:(Totpin_base32.decode p.secret) (Bytes.to_string msg)
|
||||
in
|
||||
let o = Char.code h.[String.length h - 1] land 0xf in
|
||||
let byte i = Char.code h.[o + i] in
|
||||
let n =
|
||||
((byte 0 land 0x7f) lsl 24)
|
||||
lor (byte 1 lsl 16)
|
||||
lor (byte 2 lsl 8)
|
||||
lor byte 3
|
||||
in
|
||||
Printf.sprintf "%0*d" p.digits (n mod pow10 p.digits)
|
||||
|
||||
let fail msg =
|
||||
Fmt.epr "%a@." pp (styled Style.(fg Color.red) (text msg));
|
||||
exit 1
|
||||
|
||||
let question = "Input your TOTP secret or otpauth:// URI: "
|
||||
|
||||
let clear_prompt ~len =
|
||||
if Unix.isatty Unix.stderr then begin
|
||||
let cols =
|
||||
match Console_tty.size Fmt.stderr with
|
||||
| Some size -> fst (size ())
|
||||
| None -> 80
|
||||
in
|
||||
let rows = max 1 ((String.length question + len + cols - 1) / cols) in
|
||||
Fmt.epr "\027[%dA\027[J%!" rows
|
||||
end
|
||||
|
||||
let help =
|
||||
{|Usage: totpin [-h] [-v] [--licenses]
|
||||
|
||||
Compute TOTP codes from a secret or an otpauth:// URI.
|
||||
|
||||
totpin asks for a base32 secret, or a full otpauth://totp/... URI (which also
|
||||
gives the algorithm, the number of digits and the period), without echoing it.
|
||||
Two codes are then shown, the current one and the next one, each with its
|
||||
countdown. Press any key (or Ctrl-C) to quit. Nothing is stored.
|
||||
|
||||
Options:
|
||||
-h, --help Show this help, then exit.
|
||||
-v, --version Show the version, then exit.
|
||||
--licenses Show the license of totpin and the licenses of the libraries
|
||||
it uses, then exit.
|
||||
|
||||
License: ISC, Copyright (c) 2026 Matthieu Dubuget.
|
||||
|}
|
||||
|
||||
let () =
|
||||
match List.tl (Array.to_list Sys.argv) with
|
||||
| [] -> ()
|
||||
| [ ("-h" | "--help") ] ->
|
||||
print_string help;
|
||||
exit 0
|
||||
| [ "--licenses" ] ->
|
||||
print_string Licenses.own;
|
||||
print_newline ();
|
||||
print_string Licenses.third_party;
|
||||
exit 0
|
||||
| [ ("-v" | "--version") ] ->
|
||||
print_endline Version.v;
|
||||
exit 0
|
||||
| _ ->
|
||||
prerr_string "totpin: invalid arguments\n\n";
|
||||
prerr_string help;
|
||||
exit 2
|
||||
|
||||
let () =
|
||||
Console_tty.setup ();
|
||||
Eio_main.run @@ fun env ->
|
||||
let prompt = Console_eio.Prompt.v (Eio.Stdenv.stdin env) in
|
||||
Fmt.epr "%a%!" pp (styled Style.(fg Color.green) (text question));
|
||||
match Console_eio.Prompt.secret prompt "" with
|
||||
| Error e -> fail (Fmt.str "%a" Console_eio.Prompt.pp_error e)
|
||||
| Ok input -> (
|
||||
match parse input with
|
||||
| Error msg -> fail msg
|
||||
| Ok p ->
|
||||
clear_prompt ~len:(String.length input);
|
||||
let clock = Eio.Stdenv.clock env in
|
||||
let period = p.period in
|
||||
let current = totp p ~drift:0 in
|
||||
let next = totp p ~drift:1 in
|
||||
let remaining =
|
||||
ref ((2 * period) - (int_of_float (Unix.time ()) mod period))
|
||||
in
|
||||
let row color code left =
|
||||
let left = max left 0 in
|
||||
let label = if left = 0 then "expired" else Fmt.str "%2d s" left in
|
||||
View.(
|
||||
styled
|
||||
Style.(fg color)
|
||||
(hcat ~gap:1
|
||||
[
|
||||
text code;
|
||||
Bar.horizontal ~width:30
|
||||
(float_of_int left /. float_of_int period);
|
||||
text label;
|
||||
]))
|
||||
in
|
||||
let header =
|
||||
match p.issuer with
|
||||
| Some i -> [ styled Style.bold (text i) ]
|
||||
| None -> []
|
||||
in
|
||||
let lines = List.length header + 2 in
|
||||
let frame () =
|
||||
View.vcat
|
||||
(header
|
||||
@ [
|
||||
row
|
||||
(if !remaining > period then Color.blue
|
||||
else Color.bright_black)
|
||||
current (!remaining - period);
|
||||
row Color.yellow next !remaining;
|
||||
])
|
||||
in
|
||||
let up = Fmt.str "\r\027[%dA\027[J" lines in
|
||||
Fmt.pr "%a@." View.pp (frame ());
|
||||
let clear () = Fmt.pr "%s%!" up in
|
||||
let on_signal code =
|
||||
Sys.Signal_handle
|
||||
(fun _ ->
|
||||
clear ();
|
||||
exit code)
|
||||
in
|
||||
Sys.set_signal Sys.sigint (on_signal 130);
|
||||
Sys.set_signal Sys.sigterm (on_signal 143);
|
||||
let countdown () =
|
||||
while !remaining > 0 do
|
||||
Eio.Time.sleep clock 1.0;
|
||||
decr remaining;
|
||||
Fmt.pr "%s%a@." up View.pp (frame ())
|
||||
done
|
||||
in
|
||||
let any_key () =
|
||||
match Console_eio.Prompt.keys prompt Eio.Buf_read.any_char with
|
||||
| Ok _ -> ()
|
||||
| Error _ -> Eio.Fiber.await_cancel ()
|
||||
in
|
||||
Eio.Fiber.first countdown any_key;
|
||||
clear ())
|
||||
@@ -0,0 +1,48 @@
|
||||
(lang dune 3.25)
|
||||
|
||||
(name totpin)
|
||||
|
||||
(version 1.0)
|
||||
|
||||
(generate_opam_files true)
|
||||
|
||||
(source (uri git+https://dubuget.fr/gitea/matthieu/totpin.git))
|
||||
|
||||
(homepage https://dubuget.fr/gitea/matthieu/totpin)
|
||||
|
||||
(bug_reports https://dubuget.fr/gitea/matthieu/totpin/issues)
|
||||
|
||||
(authors "Matthieu Dubuget <matthieu.dubuget@gmail.com>")
|
||||
|
||||
(maintainers "Matthieu Dubuget <matthieu.dubuget@gmail.com>")
|
||||
|
||||
(license ISC)
|
||||
|
||||
(package
|
||||
(name totpin)
|
||||
(synopsis "A cli tool to generate TOTP codes")
|
||||
(description "Nothing is stored: totpin asks for a base32 secret, or a full otpauth://totp/... URI (which also
|
||||
gives the algorithm, the number of digits and the period), without echoing it.
|
||||
Two codes are then shown, the current one and the next one, each with its
|
||||
countdown. Press any key (or Ctrl-C) to quit.")
|
||||
(depends
|
||||
(ocaml (>= 5.4))
|
||||
console
|
||||
re
|
||||
eio
|
||||
fmt
|
||||
eio_main
|
||||
console-eio
|
||||
digestif
|
||||
(bos :build)
|
||||
(ocaml-lsp-server :with-dev-setup)
|
||||
(merlin :with-dev-setup)
|
||||
(ocamlformat :with-dev-setup)
|
||||
(utop :with-dev-setup)
|
||||
(odoc :with-doc)
|
||||
)
|
||||
|
||||
(tags
|
||||
("totp" "otp" "2fa" "authentication" "command-line-utilities")
|
||||
))
|
||||
|
||||
@@ -0,0 +1,3 @@
|
||||
(executable
|
||||
(name gen_licenses)
|
||||
(libraries bos fpath re))
|
||||
@@ -0,0 +1,169 @@
|
||||
(* Prints the licence texts of the opam packages that provide the libraries
|
||||
listed in the [(libraries ...)] field of the dune file given as argument,
|
||||
and of their dependencies.
|
||||
|
||||
For each library, [ocamlfind] gives the transitive closure and the directory
|
||||
where it is installed. The opam package that owns a library is found from
|
||||
the [.changes] files of the switch, and its version and declared licence
|
||||
from the [opam] file that opam keeps for it. The licence texts are the files
|
||||
that opam installed in [doc/<package>/]. *)
|
||||
|
||||
open Bos
|
||||
|
||||
let ( let* ) = Result.bind
|
||||
let msg fmt = Printf.ksprintf (fun s -> Error (`Msg s)) fmt
|
||||
|
||||
let rec map_result f = function
|
||||
| [] -> Ok []
|
||||
| x :: xs ->
|
||||
let* y = f x in
|
||||
let* ys = map_result f xs in
|
||||
Ok (y :: ys)
|
||||
|
||||
let ocamlfind args = Cmd.(v "ocamlfind" %% of_list args)
|
||||
let lines cmd = OS.Cmd.(run_out cmd |> to_lines)
|
||||
|
||||
let line cmd =
|
||||
let* l = lines cmd in
|
||||
match l with
|
||||
| x :: _ -> Ok x
|
||||
| [] -> msg "no output from: %s" (Cmd.to_string cmd)
|
||||
|
||||
let starts_with ~prefix s =
|
||||
String.length s >= String.length prefix
|
||||
&& String.sub s 0 (String.length prefix) = prefix
|
||||
|
||||
let libraries_re = Re.Perl.compile_pat "\\(libraries([^)]*)\\)"
|
||||
|
||||
(* The libraries of the first [(libraries ...)] field of a dune file, minus
|
||||
those that findlib does not know (the libraries of the project). *)
|
||||
let roots file =
|
||||
let* s = OS.File.read (Fpath.v file) in
|
||||
match Re.exec_opt libraries_re s with
|
||||
| None -> msg "no (libraries ...) field in %s" file
|
||||
| Some g ->
|
||||
let names =
|
||||
List.filter (( <> ) "")
|
||||
(Re.split (Re.Perl.compile_pat "\\s+") (Re.Group.get g 1))
|
||||
in
|
||||
Ok
|
||||
(List.filter
|
||||
(fun lib ->
|
||||
Result.is_ok
|
||||
OS.Cmd.(
|
||||
run_out ~err:err_null (ocamlfind [ "query"; lib ]) |> to_lines))
|
||||
names)
|
||||
|
||||
let sorted_entries dir =
|
||||
let* l = OS.Dir.contents ~dotfiles:true dir in
|
||||
Ok (List.sort Fpath.compare l)
|
||||
|
||||
let load_changes switch =
|
||||
let* files = sorted_entries Fpath.(switch / ".opam-switch" / "install") in
|
||||
files
|
||||
|> List.filter (Fpath.has_ext ".changes")
|
||||
|> map_result (fun f ->
|
||||
let* content = OS.File.read f in
|
||||
Ok (Fpath.(basename (rem_ext f)), content))
|
||||
|
||||
let owner ~stdlib ~changes lib =
|
||||
let* dir = line (ocamlfind [ "query"; "-format"; "%d"; lib ]) in
|
||||
if starts_with ~prefix:stdlib dir then Ok (Some "ocaml")
|
||||
else
|
||||
let top = List.hd (String.split_on_char '.' lib) in
|
||||
let meta = Re.compile (Re.str (Printf.sprintf "lib/%s/META" top)) in
|
||||
Ok
|
||||
(List.find_map
|
||||
(fun (pkg, content) ->
|
||||
if Re.execp meta content then Some pkg else None)
|
||||
changes)
|
||||
|
||||
let license_re = Re.Perl.compile_pat ~opts:[ `Multiline ] "^license:[ \t]*(.*)$"
|
||||
|
||||
(* The version is the end of the name of the directory [<pkg>.<version>]. *)
|
||||
let package_info ~switch pkg =
|
||||
let* entries = sorted_entries Fpath.(switch / ".opam-switch" / "packages") in
|
||||
let prefix = pkg ^ "." in
|
||||
match
|
||||
List.find_opt (fun e -> starts_with ~prefix (Fpath.basename e)) entries
|
||||
with
|
||||
| None -> msg "no opam metadata for %s" pkg
|
||||
| Some e ->
|
||||
let name = Fpath.basename e in
|
||||
let version =
|
||||
String.sub name (String.length prefix)
|
||||
(String.length name - String.length prefix)
|
||||
in
|
||||
let* opam = OS.File.read Fpath.(e / "opam") in
|
||||
let license =
|
||||
match Re.exec_opt license_re opam with
|
||||
| Some g -> Re.Group.get g 1
|
||||
| None -> "unknown"
|
||||
in
|
||||
Ok (version, license)
|
||||
|
||||
let license_file ~switch pkg =
|
||||
let dir = Fpath.(switch / "doc" / pkg) in
|
||||
let* exists = OS.Dir.exists dir in
|
||||
if not exists then Ok None
|
||||
else
|
||||
let* entries = sorted_entries dir in
|
||||
Ok
|
||||
(List.find_opt
|
||||
(fun e ->
|
||||
let n = String.uppercase_ascii (Fpath.basename e) in
|
||||
starts_with ~prefix:"LICENSE" n || starts_with ~prefix:"COPYING" n)
|
||||
entries)
|
||||
|
||||
let run file =
|
||||
let* roots = roots file in
|
||||
let* destdir = line (ocamlfind [ "printconf"; "destdir" ]) in
|
||||
let switch = Fpath.(parent (v destdir)) in
|
||||
let* stdlib = line (ocamlfind [ "printconf"; "stdlib" ]) in
|
||||
let* changes = load_changes switch in
|
||||
let* libs = lines (ocamlfind ([ "query"; "-r"; "-format"; "%p" ] @ roots)) in
|
||||
let* owners =
|
||||
map_result (owner ~stdlib ~changes) (List.sort_uniq compare libs)
|
||||
in
|
||||
let packages = List.sort_uniq compare (List.filter_map Fun.id owners) in
|
||||
let* infos =
|
||||
map_result
|
||||
(fun p ->
|
||||
let* version, license = package_info ~switch p in
|
||||
let* file = license_file ~switch p in
|
||||
Ok (p, version, license, file))
|
||||
packages
|
||||
in
|
||||
print_string
|
||||
"THIRD-PARTY LICENSES\n\n\
|
||||
totpin links the following libraries. Their licence texts follow.\n";
|
||||
List.iter (fun (p, v, _, _) -> Printf.printf " - %s %s\n" p v) infos;
|
||||
let rule = String.make 64 '=' in
|
||||
let* () =
|
||||
map_result
|
||||
(fun (p, v, license, file) ->
|
||||
match file with
|
||||
| None ->
|
||||
Printf.eprintf "warning: no licence file for %s\n" p;
|
||||
Ok ()
|
||||
| Some f ->
|
||||
let* text = OS.File.read f in
|
||||
Printf.printf "\n%s\n%s %s (%s)\n%s\n\n%s" rule p v license rule
|
||||
text;
|
||||
Ok ())
|
||||
infos
|
||||
|> Result.map ignore
|
||||
in
|
||||
Ok ()
|
||||
|
||||
let () =
|
||||
match Sys.argv with
|
||||
| [| _; file |] -> (
|
||||
match run file with
|
||||
| Ok () -> ()
|
||||
| Error (`Msg m) ->
|
||||
prerr_endline ("gen_licenses: " ^ m);
|
||||
exit 1)
|
||||
| _ ->
|
||||
prerr_endline "usage: gen_licenses LIBRARIES-FILE";
|
||||
exit 2
|
||||
+48
@@ -0,0 +1,48 @@
|
||||
# This file is generated by dune, edit dune-project instead
|
||||
opam-version: "2.0"
|
||||
version: "1.0"
|
||||
synopsis: "A cli tool to generate TOTP codes"
|
||||
description: """
|
||||
Nothing is stored: totpin asks for a base32 secret, or a full otpauth://totp/... URI (which also
|
||||
gives the algorithm, the number of digits and the period), without echoing it.
|
||||
Two codes are then shown, the current one and the next one, each with its
|
||||
countdown. Press any key (or Ctrl-C) to quit."""
|
||||
maintainer: ["Matthieu Dubuget <matthieu.dubuget@gmail.com>"]
|
||||
authors: ["Matthieu Dubuget <matthieu.dubuget@gmail.com>"]
|
||||
license: "ISC"
|
||||
tags: ["totp" "otp" "2fa" "authentication" "command-line-utilities"]
|
||||
homepage: "https://dubuget.fr/gitea/matthieu/totpin"
|
||||
bug-reports: "https://dubuget.fr/gitea/matthieu/totpin/issues"
|
||||
depends: [
|
||||
"dune" {>= "3.25"}
|
||||
"ocaml" {>= "5.4"}
|
||||
"console"
|
||||
"re"
|
||||
"eio"
|
||||
"fmt"
|
||||
"eio_main"
|
||||
"console-eio"
|
||||
"digestif"
|
||||
"bos" {build}
|
||||
"ocaml-lsp-server" {with-dev-setup}
|
||||
"merlin" {with-dev-setup}
|
||||
"ocamlformat" {with-dev-setup}
|
||||
"utop" {with-dev-setup}
|
||||
"odoc" {with-doc}
|
||||
]
|
||||
build: [
|
||||
["dune" "subst"] {dev}
|
||||
[
|
||||
"dune"
|
||||
"build"
|
||||
"-p"
|
||||
name
|
||||
"-j"
|
||||
jobs
|
||||
"@install"
|
||||
"@runtest" {with-test}
|
||||
"@doc" {with-doc}
|
||||
]
|
||||
]
|
||||
dev-repo: "git+https://dubuget.fr/gitea/matthieu/totpin.git"
|
||||
x-maintenance-intent: ["(latest)"]
|
||||
@@ -0,0 +1,2 @@
|
||||
(library
|
||||
(name totpin_base32))
|
||||
@@ -0,0 +1,18 @@
|
||||
let decode s =
|
||||
let value = function
|
||||
| 'a' .. 'z' as c -> Char.code c - Char.code 'a'
|
||||
| 'A' .. 'Z' as c -> Char.code c - Char.code 'A'
|
||||
| c -> Char.code c - Char.code '2' + 26
|
||||
in
|
||||
let buf = Buffer.create 32 in
|
||||
let acc = ref 0 and bits = ref 0 in
|
||||
String.iter
|
||||
(fun c ->
|
||||
acc := (!acc lsl 5) lor value c;
|
||||
bits := !bits + 5;
|
||||
if !bits >= 8 then begin
|
||||
bits := !bits - 8;
|
||||
Buffer.add_char buf (Char.chr ((!acc lsr !bits) land 0xff))
|
||||
end)
|
||||
s;
|
||||
Buffer.contents buf
|
||||
@@ -0,0 +1,6 @@
|
||||
(** Décodage base32 (RFC 4648) pour les secrets TOTP. *)
|
||||
|
||||
val decode : string -> string
|
||||
(** [decode s] est la chaîne d'octets codée par [s], sans remplissage [=]. [s]
|
||||
ne doit contenir que des caractères de [A-Za-z2-7] : aucune validation n'est
|
||||
faite. Les bits restants à la fin sont ignorés. *)
|
||||
Reference in new issue
Block a user