From 5eb54fa81ba308b332d7f824f5c78a0b5fd735e5 Mon Sep 17 00:00:00 2001 From: Swrup Date: Tue, 30 Sep 2025 16:39:08 +0200 Subject: [PATCH] add crockford base32 --- src/base_32.ml | 24 ++++++++++++++++++++++++ src/dune | 5 +++++ 2 files changed, 29 insertions(+) create mode 100644 src/base_32.ml diff --git a/src/base_32.ml b/src/base_32.ml new file mode 100644 index 00000000..5c111967 --- /dev/null +++ b/src/base_32.ml @@ -0,0 +1,24 @@ +(* TODO test *) +(* Crockford's variant of Base32 + http://www.crockford.com/wrmg/base32.html + except that: + - 'U' is not excluded but also decodes to 'V' + - '-' is not allowed + - checksum is not allowed *) + +(* 'I' 'L' 'O' 'U' excluded *) +let alphabet = Base32.make_alphabet "0123456789ABCDEFGHJKMNPQRSTVWXYZ" +let encode s = Base32.encode_string ~alphabet s + +let decode s = + let s = + String.map + (fun c -> + match Char.uppercase_ascii c with + | 'O' -> '0' + | 'I' | 'L' -> '1' + | 'U' -> 'V' + | c -> c) + s + in + Base32.decode ~alphabet ~off:0 ~len:(String.length s) s diff --git a/src/dune b/src/dune index 19e30781..d9a560ea 100644 --- a/src/dune +++ b/src/dune @@ -21,6 +21,11 @@ (modules syntax) (libraries)) +(library + (name base_32) + (modules base_32) + (libraries base32)) + (rule (target assets_crunch.ml) (deps