EncryptionAdapter
extends IndirectAdapter
in package
implements
FilesystemAdapter, PublicUrlGenerator
Encrypts every file with XChaCha20-Poly1305 before it is handed to the underlying adapter and decrypts it again when it is read back.
Stored files use the following body format:
[version:1 byte][nonce:24 bytes][ciphertext + tag]
The additional authenticated data is path independent, which means files keep their ciphertext when they are moved or copied.
Table of Contents
Interfaces
- FilesystemAdapter
- PublicUrlGenerator
Constants
- HEADER_SIZE : int = self::PREFIX_SIZE + SODIUM_CRYPTO_AEAD_XCHACHA2...
- Total number of bytes a stored file is larger than its plaintext: the version, the nonce and the Poly1305 tag that is appended to the ciphertext.
- VERSION : int = 1
- ADDITIONAL_DATA : string = 'collecthor/flysystem-adapters/encryption'
- Fixed additional authenticated data. It is intentionally not derived from the path so that moving or copying an encrypted file does not invalidate it.
- PREFIX_SIZE : int = 1 + SODIUM_CRYPTO_AEAD_XCHACHA20POLY1305_IETF_N...
- One byte for the version and 24 bytes for the nonce, preceding the ciphertext.
Properties
Methods
- __construct() : mixed
- copy() : void
- createDirectory() : void
- delete() : void
- deleteDirectory() : void
- directoryExists() : bool
- fileExists() : bool
- fileSize() : FileAttributes
- The underlying adapter reports the size of the ciphertext. Since the header has a fixed size it is subtracted so that the reported size matches the original contents.
- lastModified() : FileAttributes
- listContents() : iterable<string|int, StorageAttributes>
- mimeType() : FileAttributes
- move() : void
- publicUrl() : string
- read() : string
- readStream() : resource
- setVisibility() : void
- visibility() : FileAttributes
- write() : void
- writeStream() : void
- getAdapter() : FilesystemAdapter
- preparePath() : string
- decrypt() : string
- encrypt() : string
- readAll() : string
- stringToStream() : resource
- stripHeader() : FileAttributes
Constants
HEADER_SIZE
Total number of bytes a stored file is larger than its plaintext: the version, the nonce and the Poly1305 tag that is appended to the ciphertext.
public
int
HEADER_SIZE
= self::PREFIX_SIZE + SODIUM_CRYPTO_AEAD_XCHACHA20POLY1305_IETF_ABYTES
VERSION
public
int
VERSION
= 1
ADDITIONAL_DATA
Fixed additional authenticated data. It is intentionally not derived from the path so that moving or copying an encrypted file does not invalidate it.
private
string
ADDITIONAL_DATA
= 'collecthor/flysystem-adapters/encryption'
PREFIX_SIZE
One byte for the version and 24 bytes for the nonce, preceding the ciphertext.
private
int
PREFIX_SIZE
= 1 + SODIUM_CRYPTO_AEAD_XCHACHA20POLY1305_IETF_NPUBBYTES
Properties
$base
private
FilesystemAdapter
$base
$key
private
string
$key
Attributes
- #[SensitiveParameter]
Methods
__construct()
public
__construct(FilesystemAdapter $base, string $key) : mixed
Parameters
- $base : FilesystemAdapter
- $key : string
copy()
public
copy(string $source, string $destination, Config $config) : void
Parameters
- $source : string
- $destination : string
- $config : Config
createDirectory()
public
createDirectory(string $path, Config $config) : void
Parameters
- $path : string
- $config : Config
delete()
public
delete(string $path) : void
Parameters
- $path : string
deleteDirectory()
public
deleteDirectory(string $path) : void
Parameters
- $path : string
directoryExists()
public
directoryExists(string $path) : bool
Parameters
- $path : string
Return values
boolfileExists()
public
fileExists(string $path) : bool
Parameters
- $path : string
Return values
boolfileSize()
The underlying adapter reports the size of the ciphertext. Since the header has a fixed size it is subtracted so that the reported size matches the original contents.
public
fileSize(string $path) : FileAttributes
Parameters
- $path : string
Return values
FileAttributeslastModified()
public
lastModified(string $path) : FileAttributes
Parameters
- $path : string
Return values
FileAttributeslistContents()
public
listContents(string $path, bool $deep) : iterable<string|int, StorageAttributes>
Parameters
- $path : string
- $deep : bool
Return values
iterable<string|int, StorageAttributes>mimeType()
public
mimeType(string $path) : FileAttributes
Parameters
- $path : string
Return values
FileAttributesmove()
public
move(string $source, string $destination, Config $config) : void
Parameters
- $source : string
- $destination : string
- $config : Config
publicUrl()
public
publicUrl(string $path, Config $config) : string
Parameters
- $path : string
- $config : Config
Return values
stringread()
public
read(string $path) : string
Parameters
- $path : string
Return values
stringreadStream()
public
readStream(string $path) : resource
Parameters
- $path : string
Return values
resourcesetVisibility()
public
setVisibility(string $path, string $visibility) : void
Parameters
- $path : string
- $visibility : string
visibility()
public
visibility(string $path) : FileAttributes
Parameters
- $path : string
Return values
FileAttributeswrite()
public
write(string $path, string $contents, Config $config) : void
Parameters
- $path : string
- $contents : string
- $config : Config
writeStream()
public
writeStream(string $path, resource $contents, Config $config) : void
Parameters
- $path : string
- $contents : resource
- $config : Config
getAdapter()
protected
getAdapter(string $rawPath, string $preparedPath) : FilesystemAdapter
Parameters
- $rawPath : string
- $preparedPath : string
Return values
FilesystemAdapterpreparePath()
protected
preparePath(string $path) : string
Parameters
- $path : string
Return values
stringdecrypt()
private
decrypt(string $path, string $payload) : string
Parameters
- $path : string
- $payload : string
Return values
stringencrypt()
private
encrypt(string $plaintext) : string
Parameters
- $plaintext : string
Return values
stringreadAll()
private
static readAll(resource $stream) : string
Parameters
- $stream : resource
Return values
stringstringToStream()
private
static stringToStream(string $contents) : resource
Parameters
- $contents : string
Return values
resourcestripHeader()
private
static stripHeader(FileAttributes $attributes) : FileAttributes
Parameters
- $attributes : FileAttributes