14 Cryptography
The crypto/ library provides Ed25519 signatures, PASETO v4.public tokens and
host-backed random generators. Keys, signatures, messages, footers and implicit
assertions are Bytes. Tokens are strings.
14.1. Secure random generators
Function(crypto/random) → generator
Creates a generator backed by the operating system's secure entropy source.
Use it with random/int, random/float, random/choose, random/shuffle and
random/bytes, with the same bounds and validation as a seeded generator:
(let [g (crypto/random)]
[(random/int g 0 100)
(random/choose g [Atreides Fremen])
(random/bytes g 32)])
Secure randomness requires permission for each evaluation, including calls to a
function that captured a generator earlier. The stardust command grants this
permission when the io plugin is explicitly selected.
Linux, macOS and Windows supply entropy; other targets report it unavailable.
Host entropy failures return #err <crypto entropy>; they never select a seeded
fallback. Empty byte requests and shuffles of zero or one items draw no entropy.
Returning or encoding a secure generator is an
error: their state cannot be serialized. Seeded generators keep their existing
reproducible sequence and #rng representation. Without a grant:
14.2. Ed25519
| Signature | Result |
|---|---|
(crypto/sign/ed25519/generate-keypair [seed]) |
{private seed public key}. With a 32-byte seed, requires stdlib and no grant. Without a seed, requires io and the secure-random grant for host entropy. |
(crypto/sign/ed25519/public-key seed) |
32-byte public key |
(crypto/sign/ed25519/sign seed message) |
64-byte signature |
(crypto/sign/ed25519/verify key message signature) |
Boolean signature validity |
(crypto/sign/ed25519/import-private bytes) |
Checked 32-byte seed |
(crypto/sign/ed25519/export-private seed) |
Checked 32-byte seed |
(crypto/sign/ed25519/import-public bytes) |
Checked public-key encoding |
(crypto/sign/ed25519/export-public key) |
Checked public-key encoding |
The private representation is the seed, not a 64-byte expanded private key.
Import/export use raw Bytes; they do not read PEM or DER. Wrong argument kinds
or lengths are errors. A correctly sized invalid signature returns false.
All operations except seedless key generation require no entropy grant.
Private seeds are ordinary Bytes: callers must keep them secret.
This example uses a public test seed. Never use it for real keys.
(let [keys (crypto/sign/ed25519/generate-keypair (encoding/decode "hex" "b4cbfb43df4ce210727d953e4a713307fa19bb7d9f85041438d9e11b942a3774")) message (encoding/encode "utf8" "House Atreides") signature (crypto/sign/ed25519/sign (get keys "private") message)] [(count signature) (crypto/sign/ed25519/verify (get keys "public") message signature) (crypto/sign/ed25519/verify (get keys "public") #b64 "" signature)])
(let [keys (crypto/sign/ed25519/generate-keypair (encoding/decode "hex" "b4cbfb43df4ce210727d953e4a713307fa19bb7d9f85041438d9e11b942a3774")) message (encoding/encode "utf8" "House Atreides") signature (crypto/sign/ed25519/sign (get keys "private") message)] [(count signature) (crypto/sign/ed25519/verify (get keys "public") message signature) (crypto/sign/ed25519/verify (get keys "public") #b64 "" signature)])
64 true false
14.3. PASETO v4.public
Only v4.public is supported. It authenticates the message and footer; both remain readable in the token. The implicit assertion is authenticated but is not included in the token.
| Signature | Result |
|---|---|
(crypto/paseto/v4/public/public-key seed) |
Ed25519 public key, using the same Bytes representation |
(crypto/paseto/v4/public/sign seed message [opts]) |
Signed token string, with optional footer or implicit assertion |
(crypto/paseto/v4/public/verify key token [opts]) |
{<verified> message bytes footer bytes} after the requested checks |
(crypto/paseto/v4/public/parse token [opts]) |
{<!verified> message bytes footer bytes}, with an optional length limit |
sign options are footer and implicit, both Bytes. verify accepts
implicit, expected-footer (Bytes), and max-length (positive integer).
Supplying an empty expected-footer requires an empty footer. parse accepts
only max-length. The default maximum input token length is 4096 bytes.
Signing does not impose that limit; raise it explicitly when verifying a
larger token. Unsupported options, malformed tokens and failed verification
return errors.
(let [seed (encoding/decode "hex" "b4cbfb43df4ce210727d953e4a713307fa19bb7d9f85041438d9e11b942a3774") token (crypto/paseto/v4/public/sign seed (encoding/encode "utf8" "Arrakis") {footer #b64 "AQ==" implicit #b64 "Ag=="})] (crypto/paseto/v4/public/verify (crypto/paseto/v4/public/public-key seed) token {expected-footer #b64 "AQ==" implicit #b64 "Ag=="}))
(let [seed (encoding/decode "hex" "b4cbfb43df4ce210727d953e4a713307fa19bb7d9f85041438d9e11b942a3774") token (crypto/paseto/v4/public/sign seed (encoding/encode "utf8" "Arrakis") {footer #b64 "AQ==" implicit #b64 "Ag=="})] (crypto/paseto/v4/public/verify (crypto/paseto/v4/public/public-key seed) token {expected-footer #b64 "AQ==" implicit #b64 "Ag=="}))
{<verified> footer #b64 "AQ==" message #b64 "QXJyYWtpcw=="}
parse does not authenticate anything. Its footer can help choose a public
key, but neither the footer nor the message is trusted until verify succeeds.
14.3.1. Claims
| Signature | Result |
|---|---|
(crypto/paseto/v4/public/sign-claims seed claims opts) |
Signed JSON object token |
(crypto/paseto/v4/public/verify-claims key token opts) |
{<verified> claims map footer bytes} |
Both operations require now as a #utc in opts; they never read the
host clock. Signing accepts footer and implicit, adds iat from now when
absent, and never invents an expiration. UTC values in exp, nbf and
iat become RFC3339 strings; the inserted iat uses whole seconds.
Verification checks claims only after signature verification. It accepts
implicit, expected-footer, max-length and these claim options:
| Option | Check |
|---|---|
issuer, subject, audience |
Nonempty string must match iss, sub or aud |
leeway |
Nonnegative #dur tolerance; defaults to zero |
<require-expiration> |
Require exp |
<require-not-before> |
Require nbf |
<require-issued-at> |
Require iat |
Present exp, nbf and iat are always validated, even without a requirement
flag. Expiration is exclusive: at exp the token is expired. nbf and iat
may equal now. Leeway relaxes these comparisons. Registered textual claims
iss, sub, aud and jti must be strings; timestamps must be complete RFC3339
strings with at most nine fractional digits. Duplicate JSON keys, including
nested keys, are rejected. Claim clocks and tolerance must fit signed 64-bit
nanoseconds (approximately years 1677–2262). Returned claims retain JSON
timestamp strings and user fields.
Claims use literal JSON, not DUST's tagged JSON transport. Keys such as
#role, ##role and $comment remain ordinary keys. Native values in
user claims become plain JSON objects: #link 42 signs as
{"#link":42} and verification returns the map {#link 42}, not a link.
Registered timestamp conversion remains as described above.
(let [seed (encoding/decode "hex" "b4cbfb43df4ce210727d953e4a713307fa19bb7d9f85041438d9e11b942a3774") token (crypto/paseto/v4/public/sign-claims seed {exp #utc "2026-01-02T00:00:00Z" sub "Paul Atreides"} {now #utc "2026-01-01T00:00:00Z"}) result (crypto/paseto/v4/public/verify-claims (crypto/paseto/v4/public/public-key seed) token {<require-expiration require-issued-at> now #utc "2026-01-01T00:00:00Z" subject "Paul Atreides"})] [(get result "verified") (get (get result "claims") "sub")])
(let [seed (encoding/decode "hex" "b4cbfb43df4ce210727d953e4a713307fa19bb7d9f85041438d9e11b942a3774") token (crypto/paseto/v4/public/sign-claims seed {exp #utc "2026-01-02T00:00:00Z" sub "Paul Atreides"} {now #utc "2026-01-01T00:00:00Z"}) result (crypto/paseto/v4/public/verify-claims (crypto/paseto/v4/public/public-key seed) token {<require-expiration require-issued-at> now #utc "2026-01-01T00:00:00Z" subject "Paul Atreides"})] [(get result "verified") (get (get result "claims") "sub")])
true "Paul Atreides"