Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source
Crockford Base32 Encoding for OCaml
An OCaml implementation of Douglas Crockford's Base32 encoding with ISO 7064 checksum support. Provides encoding and decoding of int64 values to URI-friendly base32 strings, with optional checksum validation, padding, splitting, and random ID generation. Ported from https://github.com/front-matter/commonmeta.
Installation
opam install crockford
Or add to your dune-project:
(package
(depends
(crockford)))
Usage
Basic Encoding and Decoding
(* Encode a number *)
let encoded = Crockford.encode 1234567890L in
(* "16jkpa2" *)
(* Decode back to number *)
let decoded = Crockford.decode "16jkpa2" in
(* 1234567890L *)
With Checksum
(* Encode with checksum *)
let encoded = Crockford.encode ~checksum:true 1234567890L in
(* "16jkpa2d" *)
(* Decode and validate checksum *)
let decoded = Crockford.decode ~checksum:true "16jkpa2d" in
(* 1234567890L - or raises Checksum_mismatch if invalid *)
Formatted Output
(* Split with dashes for readability *)
let encoded = Crockford.encode ~split_every:4 1234567890L in
(* "16jk-pa2" *)
(* With minimum length (zero-padded) *)
let encoded = Crockford.encode ~min_length:10 1234L in
(* "000000016j" *)
Random ID Generation
Random.self_init ();
(* Generate random IDs *)
let id = Crockford.generate ~length:8 ~checksum:true () in
(* e.g., "a3x7m9q5" *)
(* Generate formatted IDs *)
let id = Crockford.generate ~length:16 ~split_every:4 ~checksum:true () in
(* e.g., "7n2q-8xkm-5pwt-3hr9" *)
Normalization
(* Handles common character confusions *)
let decoded = Crockford.decode "ILO" in (* Treated as "110" *)
let decoded = Crockford.decode "16-JK-PA" in (* Dashes ignored *)
License
MIT License
Author
Anil Madhavapeddy anil@recoil.org (based on code from https://github.com/front-matter/commonmeta)