Skip to content

Cratis Fundamentals secrets and token handling

Use GenerateValue.Bytes or GenerateValue.NanoId in Cratis Fundamentals when a generated value grants access, such as an API key or a reset-link token. Generation is only the first step: return a bearer token once, store its digest for later verification, and keep the raw value out of logs and events. A TOTP secret, used to compute time-based one-time passwords, needs protected storage from which you can recover the original value.

Need Code Bits
API key, token, reset link GenerateValue.Bytes(32) 256
Hex token for a header or URL GenerateValue.Bytes(32, ValueEncoding.Hex) 256
TOTP secret, key material GenerateValue.Bytes(20, ValueEncoding.Base32) 160
URL-safe token, default alphabet GenerateValue.NanoId(32) 192

For bearer tokens, use at least 128 random bits: Bytes(16) meets that minimum, while Bytes(32) gives you 256. Entropy measures how much uncertainty a random value contains. Encoding changes its representation, not its entropy, so 32 bytes still carry 256 bits when encoded as base64url or hex.

Of these generation strategies, use only Bytes or NanoId for secrets. A UUID v4 has 122 random bits; UUID v7, ULID and TypeID expose their creation time and carry fewer random bits. Short, typeable invitation codes should serve as identifiers with a uniqueness check and an expiry, not as bearer credentials.

using Cratis.Concepts;
public record ApiKey(string Value) : ConceptAs<string>(Value), IGeneratable<ApiKey>
{
public static ApiKey New() => new(GenerateValue.Bytes(32));
}

In a Cratis Arc application using Cratis Chronicle, command property values are recorded in the causation chain: the metadata that says what caused an event. An unmarked ApiKey property can therefore travel with every event the command appends and remain there for as long as those events do.

[NotAudited] comes from the Cratis.Arc.Chronicle package, not Fundamentals. Put it on the concept to exclude command properties of that type from the causation chain. You can also mark a command type, a single property or its corresponding constructor parameter.

using Cratis.Arc.Chronicle.Commands;
using Cratis.Concepts;
[NotAudited]
public record ApiKey(string Value) : ConceptAs<string>(Value), IGeneratable<ApiKey>
{
public static ApiKey New() => new(GenerateValue.Bytes(32));
}

[NotAudited] has a narrow scope:

Covered Not covered
The command causation chain The payload of an event: an event that carries the token stores it in the clear
Log lines, exception messages, responses and anything else that prints the value
Encryption: the value is withheld from the chain, not protected

[PII] identifies personal data such as a name or email address for Chronicle’s compliance handling. Arc also excludes those properties from command causation. Use [NotAudited] for a secret that is not personal data; the markings serve different purposes.

For bearer tokens and API keys that callers present back to you, keep only a digest in storage and events. Use GenerateValue.Sha256(token) only for server-generated tokens with at least 128 bits of entropy, not passwords, PINs or short codes. When a token is presented, hash its text and compare the result with the stored digest. For direct comparison, use CryptographicOperations.FixedTimeEquals over equal-length digest bytes. Lookup by digest index is not constant-time and is acceptable only for these high-entropy tokens.

A TOTP secret or signing key must remain recoverable because later operations need the original value. Keep it in protected storage, such as a secret store or encrypted storage, never in an event payload.

Check logging and error paths before using either kind of secret. A concept prints its raw value from ToString(), so Console.WriteLine(apiKey) and $"{apiKey}" both write the secret. Give tokens a lifetime and a rotation path; generation and hashing do not expire them for you.

Follow SHA-256 digests to store and compare a bearer-token verifier. Use Choosing a strategy for the entropy arithmetic and the Reference for the limits of Bytes and NanoId.