To use a custom encryption backend, set the ciphersweet.backend configuration value to custom. You must then provide an invokeable factory class in the ciphersweet.backend.custom configuration key. This factory must return an object implementing ParagonIE\CipherSweet\Contract\BackendInterface.
Your implementation of BackendInterface must define the logic for encryption, decryption, blind indexing (both fast and slow), key derivation, and streaming operations.
// 1. Define your backend implementation
class CustomBackend implements BackendInterface {
public function encrypt(string $plaintext, SymmetricKey $key, string $aad = ''): string { /* ... */ }
public function decrypt(string $ciphertext, SymmetricKey $key, string $aad = ''): string { /* ... */ }
public function blindIndexFast(string $plaintext, SymmetricKey $key, ?int $bitLength = null): string { /* ... */ }
public function blindIndexSlow(string $plaintext, SymmetricKey $key, ?int $bitLength = null, array $config = []): string { /* ... */ }
public function getIndexTypeColumn(string $tableName, string $fieldName, string $indexName): string { /* ... */ }
public function deriveKeyFromPassword(string $password, string $salt): SymmetricKey { /* ... */ }
public function doStreamDecrypt($inputFP, $outputFP, SymmetricKey $key, int $chunkSize = 8192, ?AAD $aad = null): bool { /* ... */ }
public function doStreamEncrypt($inputFP, $outputFP, SymmetricKey $key, int $chunkSize = 8192, string $salt = Constants::DUMMY_SALT, ?AAD $aad = null): bool { /* ... */ }
public function getFileEncryptionSaltOffset(): int { /* ... */ }
public function getPrefix(): string { /* ... */ }
}
// 2. Create the factory
class CustomBackendFactory {
public function __invoke()
{
return new CustomBackend();
}
}
// 3. In your config file:
// 'ciphersweet.backend' => 'custom',
// 'ciphersweet.backend.custom' => CustomBackendFactory::class,