Click the Castor logo or press Ctrl Alt T to change theme.
# Base64 and hex Cryptographic values are bytes. Web applications often need text: JSON strings, URL parameters, database columns, environment variables, or HTTP headers. `SecretText` provides hex and Base64 conversions for that boundary. ## Prefer Base64 for storage and transport Base64 is compact and works well for JSON and database text columns. ```php <?php use Castor\Crypto\Base64; use Castor\Crypto\SecretText; $stored = $ciphertext->toBase64(Base64::UrlSafeNoPadding); $ciphertext = SecretText::fromBase64($stored, Base64::UrlSafeNoPadding); ``` `UrlSafeNoPadding` is convenient for tokens and URLs because it avoids `+`, `/`, and trailing `=` characters. ## Use hex when humans inspect values Hex is longer, but easy to copy, compare, and scan in logs for non-secret values such as file fingerprints. ```php $fingerprint = $hash->sum()->toHex(); $restored = SecretText::fromHex($fingerprint); ``` ## Match variants exactly The Base64 variant used for decoding must match the one used for encoding: ```php $token = $signature->toBase64(Base64::UrlSafeNoPadding); $signature = SecretText::fromBase64($token, Base64::UrlSafeNoPadding); ``` If you choose standard Base64 for a URL, routing or form decoding may change characters. If you choose URL-safe Base64 for a system expecting standard Base64, decoding may fail. ## Encoding does not declassify data Do not log an encoded secret key just because it is printable. Do not put encrypted data and the encryption key in the same row. Encoding changes representation, not confidentiality. A good mental model is: if the original bytes were secret, the encoded string is secret too.
Castor ecosystem