A modern and secure cryptography framework for PHP, offering methods for symmetric, asymmetric, hashing, key management, and more.
- Features
- Requirements
- Installation
- Usage
- Security Best Practices
- Advanced Features
- Contributing
- License
- Documentação em Português
- AES-256-GCM: Authenticated encryption with Galois/Counter Mode
- AES-256-CBC: CBC mode with HMAC for authentication
- ChaCha20-Poly1305: Modern and fast algorithm
- Camellia-256-GCM: AES alternative with equivalent security
- RSA: Implementation with OAEP padding for additional security
- ECC: Elliptic curve cryptography, more efficient for smaller keys
- ECDH: Elliptic curve Diffie-Hellman key exchange
- Argon2id: Modern algorithm recommended for passwords
- Bcrypt: Traditional algorithm for password hashing
- HMAC: Hash-based message authentication code
- KeyVault: Secure key storage with encryption
- Key Rotation: System for managing key versions
- Key Derivation: PBKDF2 and HKDF for secure derivation
- SecureFileEncryptor: File encryption with metadata
- Streaming: Efficient processing of large files
- Integrity Check: Data authentication to prevent tampering
- DatabaseEncryptor: Encryption of sensitive fields in the database
- Secure Serialization: Support for complex data types
- Authentication Context: Protection against context switching attacks
- SecureSessionHandler: Encrypted session handler
- Secure Configuration: Recommended options for cookies and sessions
- Session Hijacking Protection: HttpOnly and SameSite cookies
- Laravel: Adapter for integration with Laravel Encryption and Hashing
- Other Frameworks: Extensible interface for other frameworks
- RandomGenerator: Secure generation of random bytes, strings, and UUIDs
- SecurityAudit: Logging and auditing of cryptographic operations
- ConstantTime: Constant-time operations to prevent timing attacks
- PHP 8.1 or higher
- OpenSSL extension
- Sodium extension (recommended)
- mbstring extension
composer require vendor/crypto-framework
use CryptoFramework\CryptoFacade;
$crypto = new CryptoFacade();
// Generate a secure key
$key = $crypto->generateRandomBytes(32);
// Encrypt data
$encryptedData = $crypto->encrypt("Sensitive data", $key);
// Decrypt data
$decryptedData = $crypto->decrypt($encryptedData, $key);
use CryptoFramework\CryptoFacade;
use CryptoFramework\Factories\SymmetricCryptoFactory;
// Create symmetric cryptography factory
$factory = new SymmetricCryptoFactory();
// List available algorithms
$algorithms = $factory->getAvailableAlgorithms();
print_r($algorithms);
// Get recommended algorithm
$recommendedAlgorithm = $factory->getRecommendedAlgorithm();
// Encrypt with specific algorithm
$crypto = new CryptoFacade();
$encryptedData = $crypto->encryptWithAlgorithm("Sensitive data", $key, $recommendedAlgorithm);
use CryptoFramework\CryptoFacade;
$crypto = new CryptoFacade();
// Generate key for file encryption
$fileKey = $crypto->generateRandomBytes(32);
// Encrypt file
$metadata = [
'description' => 'Confidential document',
'owner' => 'John Doe',
'created_at' => time()
];
$crypto->encryptFile('document.pdf', 'document.enc', $fileKey, $metadata);
// Decrypt file
$retrievedMetadata = $crypto->decryptFile('document.enc', 'recovered_document.pdf', $fileKey);
use CryptoFramework\CryptoFacade;
use CryptoFramework\Database\DatabaseEncryptor;
$crypto = new CryptoFacade();
$dbKey = $crypto->generateRandomBytes(32);
$dbEncryptor = new DatabaseEncryptor();
// Encrypt sensitive fields
$userData = [
'id' => 1,
'name' => 'John Doe',
'email' => 'john@example.com',
'credit_card' => '1234-5678-9012-3456'
];
$fieldsToEncrypt = ['email', 'credit_card'];
$encryptedData = $dbEncryptor->encryptArray($userData, $dbKey, $fieldsToEncrypt);
// Store $encryptedData in the database
// Retrieve and decrypt
$retrievedData = $dbEncryptor->decryptArray($encryptedData, $dbKey, $fieldsToEncrypt);
use CryptoFramework\CryptoFacade;
use CryptoFramework\Session\SecureSessionHandler;
$crypto = new CryptoFacade();
$sessionKey = $crypto->generateRandomBytes(32);
// Configure session options
SecureSessionHandler::setup([
'cookie_lifetime' => 3600,
'gc_maxlifetime' => 3600
]);
// Create and register session handler
$sessionHandler = $crypto->createSessionHandler($sessionKey);
session_set_save_handler($sessionHandler, true);
// Start session
session_start();
// Use session normally
$_SESSION['user_id'] = 123;
use CryptoFramework\CryptoFacade;
use CryptoFramework\Adapters\LaravelAdapter;
use Illuminate\Encryption\Encrypter;
use Illuminate\Hashing\BcryptHasher;
// Create adapter for Laravel
$encrypter = new Encrypter(base64_decode($appKey), 'AES-256-CBC');
$hasher = new BcryptHasher(['rounds' => 12]);
$laravelAdapter = new LaravelAdapter($encrypter, $hasher);
// Create CryptoFacade with adapter
$crypto = new CryptoFacade(frameworkAdapter: $laravelAdapter);
// Use methods normally (will be delegated to Laravel)
$encryptedData = $crypto->encrypt("Sensitive data", "");
$hash = $crypto->hashPassword("password123");
- Key Management: Use KeyVault to securely store keys.
- Key Rotation: Rotate keys regularly to limit the impact of compromise.
- Authenticated Algorithms: Prefer algorithms that offer authentication (GCM, Poly1305).
- Associated Data: Use associated data (AAD) to bind context to encryption.
- Auditing: Implement SecurityAudit to log cryptographic operations.
- Environment Check: Use checkEnvironmentSecurity() to verify the security of the environment.
- Strong Passwords: Use SecureHasher with Argon2id for password hashing.
- Secure Sessions: Use SecureSessionHandler to protect sessions.
use CryptoFramework\CryptoFacade;
$crypto = new CryptoFacade();
$securityCheck = $crypto->checkEnvironmentSecurity();
if (!$securityCheck['php_version_secure']) {
echo "Warning: Your PHP version is not considered secure.\n";
}
if (!$securityCheck['aes_gcm_available']) {
echo "Warning: AES-GCM is not available in this environment.\n";
}
use CryptoFramework\CryptoFacade;
use CryptoFramework\Utils\SecurityAudit;
use Monolog\Logger;
use Monolog\Handler\StreamHandler;
// Configure logger
$logger = new Logger('crypto');
$logger->pushHandler(new StreamHandler('crypto.log', Logger::INFO));
// Create security audit
$securityAudit = new SecurityAudit($logger, 'MyApp', 'admin');
// Create CryptoFacade with auditing
$crypto = new CryptoFacade(securityAudit: $securityAudit);
// All operations will be logged
$encryptedData = $crypto->encrypt("Sensitive data", $key);
Contributions are welcome! Please feel free to submit a Pull Request.
This project is licensed under the MIT License - see the LICENSE file for details.
- AES-256-GCM: Criptografia autenticada com modo Galois/Counter
- AES-256-CBC: Modo CBC com HMAC para autenticação
- ChaCha20-Poly1305: Algoritmo moderno e rápido
- Camellia-256-GCM: Alternativa ao AES com segurança equivalente
- RSA: Implementação com padding OAEP para segurança adicional
- ECC: Criptografia de curva elíptica, mais eficiente para chaves menores
- ECDH: Troca de chaves Diffie-Hellman de curva elíptica
- Argon2id: Algoritmo moderno recomendado para senhas
- Bcrypt: Algoritmo tradicional para hash de senhas
- HMAC: Código de autenticação de mensagem baseado em hash
- KeyVault: Armazenamento seguro de chaves com criptografia
- Rotação de Chaves: Sistema para gerenciar versões de chaves
- Derivação de Chaves: PBKDF2 e HKDF para derivação segura
- SecureFileEncryptor: Criptografia de arquivos com metadados
- Streaming: Processamento eficiente de arquivos grandes
- Verificação de Integridade: Autenticação de dados para prevenir adulteração
- DatabaseEncryptor: Criptografia de campos sensíveis no banco de dados
- Serialização Segura: Suporte para tipos de dados complexos
- Contexto de Autenticação: Proteção contra ataques de troca de contexto
- SecureSessionHandler: Manipulador de sessão criptografado
- Configuração Segura: Opções recomendadas para cookies e sessões
- Proteção contra Sequestro de Sessão: Cookies HttpOnly e SameSite
- Laravel: Adaptador para integração com Criptografia e Hash do Laravel
- Outros Frameworks: Interface extensível para outros frameworks
- RandomGenerator: Geração segura de bytes aleatórios, strings e UUIDs
- SecurityAudit: Registro e auditoria de operações criptográficas
- ConstantTime: Operações de tempo constante para prevenir ataques de temporização
- PHP 8.1 ou superior
- Extensão OpenSSL
- Extensão Sodium (recomendado)
- Extensão mbstring
composer require vendor/crypto-framework
use CryptoFramework\CryptoFacade;
$crypto = new CryptoFacade();
// Gerar uma chave segura
$key = $crypto->generateRandomBytes(32);
// Criptografar dados
$encryptedData = $crypto->encrypt("Dados sensíveis", $key);
// Descriptografar dados
$decryptedData = $crypto->decrypt($encryptedData, $key);
use CryptoFramework\CryptoFacade;
use CryptoFramework\Factories\SymmetricCryptoFactory;
// Criar fábrica de criptografia simétrica
$factory = new SymmetricCryptoFactory();
// Listar algoritmos disponíveis
$algorithms = $factory->getAvailableAlgorithms();
print_r($algorithms);
// Obter algoritmo recomendado
$recommendedAlgorithm = $factory->getRecommendedAlgorithm();
// Criptografar com algoritmo específico
$crypto = new CryptoFacade();
$encryptedData = $crypto->encryptWithAlgorithm("Dados sensíveis", $key, $recommendedAlgorithm);
use CryptoFramework\CryptoFacade;
$crypto = new CryptoFacade();
// Gerar chave para criptografia de arquivo
$fileKey = $crypto->generateRandomBytes(32);
// Criptografar arquivo
$metadata = [
'description' => 'Documento confidencial',
'owner' => 'João Silva',
'created_at' => time()
];
$crypto->encryptFile('documento.pdf', 'documento.enc', $fileKey, $metadata);
// Descriptografar arquivo
$retrievedMetadata = $crypto->decryptFile('documento.enc', 'documento_recuperado.pdf', $fileKey);
use CryptoFramework\CryptoFacade;
use CryptoFramework\Database\DatabaseEncryptor;
$crypto = new CryptoFacade();
$dbKey = $crypto->generateRandomBytes(32);
$dbEncryptor = new DatabaseEncryptor();
// Criptografar campos sensíveis
$userData = [
'id' => 1,
'name' => 'João Silva',
'email' => 'joao@exemplo.com',
'credit_card' => '1234-5678-9012-3456'
];
$fieldsToEncrypt = ['email', 'credit_card'];
$encryptedData = $dbEncryptor->encryptArray($userData, $dbKey, $fieldsToEncrypt);
// Armazenar $encryptedData no banco de dados
// Recuperar e descriptografar
$retrievedData = $dbEncryptor->decryptArray($encryptedData, $dbKey, $fieldsToEncrypt);
use CryptoFramework\CryptoFacade;
use CryptoFramework\Session\SecureSessionHandler;
$crypto = new CryptoFacade();
$sessionKey = $crypto->generateRandomBytes(32);
// Configurar opções de sessão
SecureSessionHandler::setup([
'cookie_lifetime' => 3600,
'gc_maxlifetime' => 3600
]);
// Criar e registrar manipulador de sessão
$sessionHandler = $crypto->createSessionHandler($sessionKey);
session_set_save_handler($sessionHandler, true);
// Iniciar sessão
session_start();
// Usar sessão normalmente
$_SESSION['user_id'] = 123;
use CryptoFramework\CryptoFacade;
use CryptoFramework\Adapters\LaravelAdapter;
use Illuminate\Encryption\Encrypter;
use Illuminate\Hashing\BcryptHasher;
// Criar adaptador para Laravel
$encrypter = new Encrypter(base64_decode($appKey), 'AES-256-CBC');
$hasher = new BcryptHasher(['rounds' => 12]);
$laravelAdapter = new LaravelAdapter($encrypter, $hasher);
// Criar CryptoFacade com adaptador
$crypto = new CryptoFacade(frameworkAdapter: $laravelAdapter);
// Usar métodos normalmente (serão delegados ao Laravel)
$encryptedData = $crypto->encrypt("Dados sensíveis", "");
$hash = $crypto->hashPassword("senha123");
- Gerenciamento de Chaves: Use KeyVault para armazenar chaves de forma segura.
- Rotação de Chaves: Rotacione chaves regularmente para limitar o impacto de comprometimento.
- Algoritmos Autenticados: Prefira algoritmos que ofereçam autenticação (GCM, Poly1305).
- Dados Associados: Use dados associados (AAD) para vincular contexto à criptografia.
- Auditoria: Implemente SecurityAudit para registrar operações criptográficas.
- Verificação de Ambiente: Use checkEnvironmentSecurity() para verificar a segurança do ambiente.
- Senhas Fortes: Use SecureHasher com Argon2id para hash de senhas.
- Sessões Seguras: Use SecureSessionHandler para proteger sessões.
use CryptoFramework\CryptoFacade;
$crypto = new CryptoFacade();
$securityCheck = $crypto->checkEnvironmentSecurity();
if (!$securityCheck['php_version_secure']) {
echo "Aviso: Sua versão do PHP não é considerada segura.\n";
}
if (!$securityCheck['aes_gcm_available']) {
echo "Aviso: AES-GCM não está disponível neste ambiente.\n";
}
use CryptoFramework\CryptoFacade;
use CryptoFramework\Utils\SecurityAudit;
use Monolog\Logger;
use Monolog\Handler\StreamHandler;
// Configurar logger
$logger = new Logger('crypto');
$logger->pushHandler(new StreamHandler('crypto.log', Logger::INFO));
// Criar auditoria de segurança
$securityAudit = new SecurityAudit($logger, 'MeuApp', 'admin');
// Criar CryptoFacade com auditoria
$crypto = new CryptoFacade(securityAudit: $securityAudit);
// Todas as operações serão registradas
$encryptedData = $crypto->encrypt("Dados sensíveis", $key);
Este projeto está licenciado sob a Licença MIT - veja o arquivo LICENSE para detalhes.