Skip to content

routineco/ocaml-nanoid

Repository files navigation

Nano ID for OCaml %%VERSION%%

OCaml-CI Build Status

This is an implementation of Nano ID, a tiny, secure, URL-friendly, unique string ID generator.

API

The API in documented in the source code. It mimics the Javascript interface and is mostly composed of a single entrypoint which generates a Nano ID of the given size, which defaults to 21.

val nanoid : ?size:int -> unit -> string

Usage

Nanoid supports both native and js_of_ocaml environments via a virtual library: nanoid. Libraries should depend on this library, and final executables should pick an implementation by depending either on nanoid.os or nanoid.jsoo.

Unsupported features

Custom alphabets are not yet supported.

Native implementation

The native implementation mimics the javascript one and relies by default on cryptokit pseudo random number generator seeded with the current unix time as given by Unix.gettimeofday. This should yield satisfying results for most application without ever blocking, unless you need strong cryptographic guarantees for security reasons.

One can customize the random number generation by using the nanoid_os library instead. The generated module will only work in native code. One can regain this genericity by functorizing over the Nanoid_os.S interface. The setup used in nanoid.os is the one generated by Simple ().