Hashing and security filters
Cryptographic digests and password hashes. All are Pagelove extensions.
sha256
Produces a hex-encoded SHA-256 hash of the input string.
{{ "hello" | sha256 }}
Output: 2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824
bcrypt
Produces a bcrypt hash of the input string. The optional argument sets the cost, from 4 to 31 (default 12). Each step of cost doubles the time. bcrypt reads only the first 72 bytes of the input.
{{ "password" | bcrypt }}
{{ "password" | bcrypt: 10 }}
argon2
Produces an Argon2id hash of the input string. Returns a PHC-format string by default.
{{ "password" | argon2 }}
{{ "password" | argon2: memory: 65536, time: 3 }}
| Parameter | Default | Description |
|---|---|---|
format |
"phc" |
Output format: "phc" (standard PHC string) or "raw" (hex-encoded) |
salt |
— | Salt to use in "raw" mode. Required when format: "raw"; at least 8 bytes. Ignored in PHC mode. |
memory |
19456 |
Memory cost in KiB, from 8 to 262,144 (256 MiB) |
time |
2 |
Time cost / iterations (minimum 1) |
length |
32 |
Output hash length in bytes (4–64) |
The default PHC output is self-describing: the string embeds the salt and parameters, so it can be verified later (re-hash the candidate and compare). format: "raw" returns only the hex-encoded digest bytes, which have nowhere to carry a salt — so raw mode requires an explicit salt: (otherwise the random salt would be discarded, leaving a digest that can never be reproduced). Supply a stable salt so the value is deterministic:
{{ "password" | argon2: format: "raw", salt: "per-user-unique-salt" }}
For password storage, prefer the default PHC format unless you are managing salts yourself.
How long hashing takes
bcrypt and argon2 are slow on purpose, and once hashing starts it cannot be stopped. So before it starts, Pagelove estimates how long the work will take and makes the filter fail if it would take longer than the time left in the request's budget. When the request has no time budget, a template rendered from a Trigger or Processor shares the 5 seconds that Trigger or Processor has, and anywhere else each use of the filter gets up to 5 seconds of its own. The defaults take well under that. Lower the cost, memory or time if a filter fails for this reason.
The server also runs only as many Argon2 and bcrypt hashes at once as it has processor cores. One site may use at most half of those slots, and at least one. On a server with 2 or more slots, that stops one site taking them all. If your site is already using its share, the filter is refused at once. Otherwise a hash waits for a free slot only while the time left still covers the hash. If no slot comes free in that time, or your site reaches its share while the hash waits, the filter is refused. Either way the page does not render. The client gets 503 Service Unavailable with a Retry-After: 1 header, and the error says the server, or your site, is already running as many hashes as it may and to try again shortly. The filter's inline error marker is not used. The same holds for a filter you write in Sessel: a busy refusal, or a refusal because the server cannot tell which site the call is for, stops the render. The client gets the status that fault gets everywhere else. A mistake that is the template's own, such as a cost out of range or a lambda that divides by zero, still shows as the inline error marker. This refusal is about load on the server, not your settings. Try again shortly. A template rendered by a Trigger or Processor gets the same answer: the client gets that Trigger's or Processor's own error document, with status 503 and a Retry-After: 1 header. The one exception is a Processor refusal after the request's save was stored: it is 503 with no Retry-After header, because a retry would store the save again.
Argon2's memory counts against the request's memory budget: about 19 MiB at the defaults, so a host whose memory budget is under about 20 MB cannot render the default argon2 filter. One server process runs at most 1 GiB of Argon2 memory at once, and each site at most half of that (512 MiB). A filter that would go past either limit is refused in the same way, with a 503 and Retry-After: 1, and can be tried again. This holds for a filter your site defines in Sessel too, such as the standard schemas' argon2: it draws on the request's budget like any other.
Sessel's bcrypt() and argon2() and server JavaScript's crypto.subtle Argon2 and bcrypt use the same code and the same limits.
See also
- Random generation filters — generate salts, tokens, and passphrases
- When a filter errors