add crockford base32

This commit is contained in:
Swrup 2025-09-30 16:39:08 +02:00
parent 6f85658845
commit 5eb54fa81b
2 changed files with 29 additions and 0 deletions

24
src/base_32.ml Normal file
View file

@ -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