Nanoid.Secure (Nanoid v3.0.0-rc.1)

Copy Markdown View Source

Generate a secure URL-friendly unique ID using :crypto.strong_rand_bytes/1.

By default the ID has 21 symbols, with a collision probability similar to UUID v4.

Use generate/0 for the all-defaults shortcut, or generate_with/1 with :size and/or :alphabet for custom IDs. The positional generate/1,2 variants remain available for backward compatibility but are deprecated.

Summary

Functions

Generates a secure NanoID using the default size and alphabet.

generate(size) deprecated

Generates a secure NanoID with the given size.

Generates a secure NanoID using a custom size and an individual alphabet.

Generates a secure NanoID using a keyword list of options.

Functions

generate()

@spec generate() :: binary()

Generates a secure NanoID using the default size and alphabet.

Quick-access shortcut, equivalent to generate_with([]).

Example

iex> Nanoid.Secure.generate()
"mJUHrGXZBZpNX50x2xkzf"

generate(size)

This function is deprecated. Use Nanoid.Secure.generate_with/1 instead.
@spec generate(non_neg_integer()) :: binary()

Generates a secure NanoID with the given size.

Deprecated — use generate_with(size: size).

generate(size, alphabet)

This function is deprecated. Use Nanoid.Secure.generate_with/1 instead.
@spec generate(non_neg_integer(), binary()) :: binary()

Generates a secure NanoID using a custom size and an individual alphabet.

Deprecated — use generate_with/1 with the :size and :alphabet options instead:

Nanoid.Secure.generate_with(size: 12, alphabet: "abcdef123")

generate_with(opts)

@spec generate_with(keyword()) :: binary()

Generates a secure NanoID using a keyword list of options.

Always pass at least one option — for the no-options shortcut, use generate/0.

Options

Examples

iex> Nanoid.Secure.generate_with(size: 16)
"IRFa-VaY2b-NU5xX"

iex> Nanoid.Secure.generate_with(alphabet: "abcdef123")
"d1dcd2dee333cae1bfdea"

iex> Nanoid.Secure.generate_with(size: 12, alphabet: "abcdef123")
"d1dcd2dee333"