PHP 7.2+ ships with the libsodium extension built-in. Sodium provides modern, audited cryptographic primitives: authenticated encryption, digital signatures, key exchange, and password hashing. This lab covers all major Sodium functions with real verification.
Step 1: Sodium Overview
<?php// libsodium is always available in PHP 7.2+echo"Sodium version: ".SODIUM_LIBRARY_VERSION."\n";echo"Major: ".SODIUM_LIBRARY_MAJOR_VERSION."\n";// Key sizesecho"\n=== Key Sizes ===\n";echo"secretbox key: ".SODIUM_CRYPTO_SECRETBOX_KEYBYTES." bytes\n";echo"secretbox nonce: ".SODIUM_CRYPTO_SECRETBOX_NONCEBYTES." bytes\n";echo"sign public key: ".SODIUM_CRYPTO_SIGN_PUBLICKEYBYTES." bytes\n";echo"sign secret key: ".SODIUM_CRYPTO_SIGN_SECRETKEYBYTES." bytes\n";echo"box public key: ".SODIUM_CRYPTO_BOX_PUBLICKEYBYTES." bytes\n";echo"pwhash salt: ".SODIUM_CRYPTO_PWHASH_SALTBYTES." bytes\n";echo"generichash key: ".SODIUM_CRYPTO_GENERICHASH_KEYBYTES." bytes\n";
💡 Use SENSITIVE ops/mem for highly sensitive data (private keys, HSM-grade). Use INTERACTIVE for login flows where UX matters. Never use less than INTERACTIVE.
Step 6: BLAKE2b Hashing & MACs
Step 7: Secure Random & Memory Safety
Step 8: Capstone — Encrypted JWT-Style Token System