Integration decisions
Move from an @zelktools/core recipe to an integration that handles bytes, credentials and errors correctly.
Start with the 12 library recipes for installation and examples. This guide covers the decisions you make when connecting those recipes to a form, database or API.
What do I deliver and what do I store?
| Scenario | Deliver | Keep |
|---|---|---|
| Account signup | The password travels to the server over HTTPS during signup. | The entire encoded string from hashPassword, never the password. Keep the salt and parameters with the hash. |
| Invitation | The random token goes to the recipient. Anyone holding the link can attempt redemption. | Token digest, recipient, expiration and redemption status. Two concurrent requests must not consume the same invitation. |
| Outgoing webhook | Send the body and HMAC tag to the receiver. | Both parties keep the shared key. A public digest cannot replace a keyed tag. |
| Job correlation | Send a UUID to the service handling the job. | Attach that ID to its events. UUID v7 reveals approximate creation time and does not guarantee ordering within a millisecond. |
The difference between the first two rows is how the secret was chosen. Human passwords can be predictable and require an expensive function such as scrypt. A token drawn from 32 random bytes is not susceptible to dictionary guessing; a SHA-256 digest makes a useful lookup key without storing the token.
Bytes, characters and representations
generateToken({ bytes: 32, ... }) generates 32 bytes, not 32 printable characters. The same bytes take 64 characters in hex, 44 in padded Base64, or 43 in unpadded Base64URL. Changing representation adds no randomness. Size a database column for the representation you store.
import { generateRandomBytes, encodeBytes, decodeBytes } from "@zelktools/core";
import { nodeRandom } from "@zelktools/core/node";
const raw = generateRandomBytes(32, nodeRandom);
const printable = encodeBytes(raw, "base64url");
const restored = decodeBytes(printable, "base64url");
// printable.length === 43; restored contains the same 32 bytes as raw.If an API supplies a hex HMAC key, use keyEncoding: "hex". The string "6162" represents two bytes in hex but four in UTF-8. Both inputs are valid: choosing the wrong encoding gives a valid tag for a different key. A trailing newline or reserialized JSON can likewise change the message bytes.
Enforce a password policy without silently weakening it
Use generatePasswords to generate several passwords under one policy. This example excludes ambiguous characters for easier transcription and requires two digits in each result.
import { generatePasswords, type PasswordOptions } from "@zelktools/core";
import { browserRandom } from "@zelktools/core/browser";
const policy = {
length: 32, uppercase: true, lowercase: true, numbers: true, symbols: true,
minimumNumbers: 2, excludeAmbiguous: true,
} satisfies PasswordOptions;
const suggestions = generatePasswords(policy, 3, browserRandom);Each result has its own value; these are not three pieces of one password. The batch size is 1–20. If the minimum counts exceed the password length, surface the configuration error rather than retrying with a weaker policy. In React, generate from the button handler, not during rendering.
Limits that affect application design
| Operation | Relevant limit | Practical decision |
|---|---|---|
| Digest or checksum comparison | 1 MiB of input per call | For larger files, use an incremental implementation. Concatenating chunk digests does not produce the file digest. |
| HMAC | 1–4096 key bytes; messages up to 1 MiB after decoding | Check the byte size and encoding contract before accepting a body. |
| Password hashing | Up to 1024 UTF-8 bytes of password | Do not confuse string.length with byte length or truncate the value: that changes the credential. |
| Passphrase | 3–20 words from the English list | Six independent draws from 2048 words provide 66 bits before extras. A fixed separator improves readability but adds no entropy. |
For concurrent logins, measure hashPassword and verifyPassword on the actual server. An asynchronous call still uses CPU and memory. Your application controls concurrency and login attempts; the package bounds the cost of each operation.
Distinguish a mismatch from an error
import { verifyDigest, ZelkError } from "@zelktools/core";
export async function compareDownload(bytes, expectedHex) {
try {
const { matches } = await verifyDigest({
algorithm: "SHA-256", input: bytes, expected: expectedHex, encoding: "hex",
});
return { status: matches ? "match" : "mismatch" };
} catch (error) {
if (!(error instanceof ZelkError)) throw error;
return { status: "error", code: error.code };
}
}mismatch means the comparison completed and the bytes differ. error means no valid comparison took place: the expected digest might be malformed or the crypto runtime unavailable. Do not label both cases “file modified”. At login, similarly distinguish matches: false from a stored verifier that cannot be parsed.
Test your integration
- Deterministic transformations: check a known answer. UTF-8
caféencodes to636166c3a9in hex. Also check that a round trip preserves spaces and newlines. - Generators: assert length, alphabet and class minimums rather than a specific token. Use the package's cryptographic adapters in production.
- Persistence: save and reload
encodedbefore verifying. Try both the correct and an incorrect password; this can catch truncation or transformations in the storage layer. - Protocols: check a synthetic webhook against a known vector or the receiver's implementation. Two functions with the same encoding mistake can agree with each other.