SquatchMail.PublicId (SquatchMail v0.1.0)

Copy Markdown View Source

Generates prefixed, URL-safe public identifiers backed by random bytes.

SquatchMail rows use opaque bigserial primary keys internally, but expose a stable, non-enumerable public_id (e.g. "em_4f9Kd2...") in URLs and APIs so that internal row counts never leak. The random suffix is a base62 encoding of cryptographically strong random bytes, keeping ids short and copy/paste-safe (no -, _, +, or /).

This module has no external dependencies; it relies only on :crypto.strong_rand_bytes/1.

Summary

Functions

Encodes a binary as a base62 string. An all-zero binary encodes to "0".

Generates a prefixed public id, e.g. generate("em") => "em_9aXd2Kf...".

Returns a base62 string derived from bytes cryptographically random bytes.

Functions

encode(binary)

@spec encode(binary()) :: String.t()

Encodes a binary as a base62 string. An all-zero binary encodes to "0".

generate(prefix, bytes \\ 16)

@spec generate(String.t(), pos_integer()) :: String.t()

Generates a prefixed public id, e.g. generate("em") => "em_9aXd2Kf...".

bytes controls the amount of entropy (default 16 bytes / 128 bits).

random_base62(bytes)

@spec random_base62(pos_integer()) :: String.t()

Returns a base62 string derived from bytes cryptographically random bytes.

Useful for opaque tokens (e.g. per-source webhook tokens) that don't need a human-facing prefix.