PHP code example of tourze / quic-crypto

1. Go to this page and download the library: Download tourze/quic-crypto library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

tourze / quic-crypto example snippets



Tourze\QUIC\Crypto\AES128GCM;
use Tourze\QUIC\Crypto\KeyDerivation;
use Tourze\QUIC\Crypto\SecureRandom;

// 1. 生成安全的加密密钥
$key = SecureRandom::generateAESKey(128);

// 2. 创建 AEAD 加密实例
$aes = new AES128GCM($key);

// 3. 加密数据
$plaintext = 'Hello, QUIC!';
$nonce = SecureRandom::generateNonce(12);
$aad = 'additional authenticated data';
$ciphertext = $aes->encrypt($plaintext, $nonce, $aad);

// 4. Decrypt data
$decrypted = $aes->decrypt($ciphertext, $nonce, $aad);
echo $decrypted; // Output: Hello, QUIC!

// 5. Key derivation example
$secret = SecureRandom::generate(32);
$derivedKey = KeyDerivation::quicKDF($secret, 'quic key', 16);

use Tourze\QUIC\Crypto\AES128GCM;

// Create AES-128-GCM instance
$key = random_bytes(16);
$aes = new AES128GCM($key);

// Encrypt data
$plaintext = 'Hello, QUIC!';
$nonce = random_bytes(12);
$aad = 'additional data';
$ciphertext = $aes->encrypt($plaintext, $nonce, $aad);

// Decrypt data
$decrypted = $aes->decrypt($ciphertext, $nonce, $aad);

use Tourze\QUIC\Crypto\KeyDerivation;

// HKDF key derivation
$ikm = 'input key material';
$salt = 'optional salt';
$info = 'context info';
$length = 32;

$derivedKey = KeyDerivation::hkdf($ikm, $length, $info, $salt);

// QUIC-specific key derivation
$secret = random_bytes(32);
$label = 'quic key';
$quicKey = KeyDerivation::quicKDF($secret, $label, 16);

use Tourze\QUIC\Crypto\HeaderProtection;
use Tourze\QUIC\Crypto\AES128GCM;

$aesKey = random_bytes(16);
$hpKey = random_bytes(16);
$aes = new AES128GCM($aesKey);

$hp = new HeaderProtection($aes, $hpKey);

// Protect header
$header = "\x40\x01\x02\x03\x04";
$sample = random_bytes(16);
$protectedHeader = $hp->protect($header, $sample);

// Unprotect header
$originalHeader = $hp->unprotect($protectedHeader, $sample);

use Tourze\QUIC\Crypto\CryptoSuite;

// Create crypto suite
$key = random_bytes(16);
$suite = CryptoSuite::createAES128GCM($key);

// Get suite information
echo $suite->getName(); // TLS_AES_128_GCM_SHA256
echo $suite->getKeyLength(); // 16
echo $suite->getHashAlgorithm(); // sha256

// Check supported suites
$supported = CryptoSuite::getSupportedSuites();
$best = CryptoSuite::selectBestSuite();

use Tourze\QUIC\Crypto\KeyManager;
use Tourze\QUIC\Crypto\CryptoSuite;

$suite = CryptoSuite::createAES128GCM(random_bytes(16));
$keyManager = new KeyManager($suite);

// Derive initial secrets
$connectionId = random_bytes(8);
$secrets = $keyManager->deriveInitialSecrets($connectionId);

// Get current keys
$writeKey = $keyManager->getWriteKey(true); // Client key
$writeIv = $keyManager->getWriteIv(true);
$hpKey = $keyManager->getHpKey(true);

// Construct nonce
$packetNumber = 12345;
$nonce = $keyManager->constructNonce($writeIv, $packetNumber);

use Tourze\QUIC\Crypto\SecureRandom;

// Generate random bytes
$randomBytes = SecureRandom::generate(32);

// Generate nonce
$nonce = SecureRandom::generateNonce(12);

// Generate keys
$aesKey = SecureRandom::generateAESKey(128); // AES-128
$chachaKey = SecureRandom::generateChaCha20Key(); // ChaCha20

// Generate connection ID
$connectionId = SecureRandom::generateConnectionId(8);

// Generate UUID
$uuid = SecureRandom::generateUuid();

// Timing-safe comparison
$equal = SecureRandom::timingSafeEquals($string1, $string2);

use Tourze\QUIC\Crypto\Exception\CryptoException;

try {
    $ciphertext = $aes->encrypt($plaintext, $nonce, $aad);
} catch (CryptoException $e) {
    echo 'Encryption failed: ' . $e->getMessage();
    echo 'Error code: ' . $e->getCode();
}