# Crockford Base32 Encoding for OCaml

An OCaml implementation of [Douglas Crockford's
Base32](https://www.crockford.com/base32.html) 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

```bash
opam install crockford
```

Or add to your `dune-project`:

```scheme
(package
  (depends
    (crockford)))
```

## Usage

### Basic Encoding and Decoding

```ocaml
(* Encode a number *)
let encoded = Crockford.encode 1234567890L in
(* "16jkpa2" *)

(* Decode back to number *)
let decoded = Crockford.decode "16jkpa2" in
(* 1234567890L *)
```

### With Checksum

```ocaml
(* 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

```ocaml
(* 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

```ocaml
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

```ocaml
(* 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)

## Links

- [Homepage](https://tangled.org/@anil.recoil.org/ocaml-crockford)
- [Crockford Base32 Specification](https://www.crockford.com/base32.html)
