$npx -y skills add Lonsdale201/wp-agent-skills --skill br-cryptoUse Better Route 1.1 cryptographic helpers for secure random tokens, Hex/Base64/Base64URL encoding, strict Base64URL decoding, and constant-time secret comparison. Use when implementing nonces, state, PKCE, opaque tokens, or signature comparisons.
| 1 | # Better Route crypto helpers |
| 2 | |
| 3 | Use the library helpers instead of reimplementing small security primitives. |
| 4 | |
| 5 | ```php |
| 6 | use BetterRoute\Support\Crypto; |
| 7 | use BetterRoute\Support\CryptoEncoding; |
| 8 | |
| 9 | $state = Crypto::token(32); // Base64URL by default. |
| 10 | $nonce = Crypto::token(32, CryptoEncoding::Base64Url); |
| 11 | $hex = Crypto::tokenHex(32); |
| 12 | |
| 13 | $encoded = Crypto::base64UrlEncode($raw); |
| 14 | $decoded = Crypto::base64UrlDecode($encoded); |
| 15 | |
| 16 | if (!Crypto::equals($expected, $provided)) { |
| 17 | throw new \BetterRoute\Http\ApiException('Invalid token.', 401, 'invalid_token'); |
| 18 | } |
| 19 | ``` |
| 20 | |
| 21 | ## Rules |
| 22 | |
| 23 | - Pass entropy in bytes, not output-character count. The default 32 bytes provides 256 bits before encoding. |
| 24 | - `Crypto::token()` uses `random_bytes()` and accepts `CryptoEncoding::Hex`, `Base64`, or `Base64Url`, including their lowercase string values. |
| 25 | - `Crypto::base64UrlDecode()` validates alphabet, padding placement, length, and decoder success; catch `RuntimeException` at an input boundary if malformed input should become a client error. |
| 26 | - Use `Crypto::equals()` only with strings of the expected representation. Decode/normalize representations before comparing, but never perform lossy case normalization on secret material. |
| 27 | - Use `br-single-use-token` when a token must also be consumed atomically, `br-hmac-signature` for request signing, and `br-jwks-jwt-auth` for JWTs. |
| 28 | |
| 29 | Do not use these helpers as password hashing, encryption, key derivation, or a substitute for a protocol-specific verifier. |
| 30 | |
| 31 | Source references: `src/Support/Crypto.php`, `src/Support/CryptoEncoding.php`. |
| 32 | |
| 33 | ## References |
| 34 | |
| 35 | - Official documentation: <https://lonsdale201.github.io/better-docs/docs/better-route/agents> |