Documentations › mmo.edms › php › helpers Cipher MMO\edms\helpers · 1 symbol · generated 30 Nov 2026
The encryption at rest of the bytes of a file (FileManager::$encryption ): libsodium's XChaCha20-Poly1305, a chunk at a time, so a file is never held in memory and a part of it is read without the rest (a Range request, CipherStream ).
class MMO\edms\helpers\Cipher Namespace MMO\edms\helpersWhat is written: a header -- `MMOEDMS1`, the size of a chunk (4 bytes, big endian), 16 random bytes -- then each
chunk encrypted, with its 16 bytes of tag. The nonce of chunk `i` is the 16 random bytes followed by `i` (8 bytes):
no two chunks of any file share one. Each chunk authenticates the header, its number and whether it is the last
one: a chunk changed, moved, or a file cut short is refused, never read as something else.
The key (32 bytes) is in a file of its own, never in the database ([`FromKeyFile`](https://mmoframework.fr/en/documentation/mmo.edms/php/helpers/Cipher.md#cipher-fromkeyfile)). $cipher = Cipher::FromKeyFile('{configs}/edms.key');
$cipher->encryptFile('/tmp/scan.pdf', '/tmp/scan.pdf.enc');
$handle = $cipher->decrypt(fopen('/tmp/scan.pdf.enc', 'rb')); // reads the plain bytes
Index CHUNK_SIZE HEADER_SIZE MAGIC TAG_SIZE key additionalData($header, $i, $last) decrypt($handle) decryptChunk($header, $i, $encrypted, $last) encryptChunk($header, $i, $plain, $last) encryptFile($source, $target) Encrypt the local file $source into $target encryptStream($in, $out) Encrypt what $in holds, from where it is, into $out FromKeyFile($filename) static The key of $filename: its 32 bytes as they are, or written in base64 or hexadecimal (head -c 32 /dev/urandom | base64) keyId() nonce($header, $i) PlainSize($size, $chunkSize) static readChunk($handle, $length) readHeader($handle) write($handle, $data) __construct($key)
Constants4 const CHUNK_SIZE = 65536 const MAGIC = 'MMOEDMS1' const TAG_SIZE = 16 Properties1 protected string $key Type string Methods14 protected function additionalData(string $header, int $i, bool $last): string function decrypt($handle) $handle the encrypted bytes (a storage's), the stream returned closes it returns a stream of the plain bytes: it reads, seeks, and its fstat() gives their size throws if $handle holds no bytes this class wrote
function decryptChunk(string $header, int $i, string $encrypted, bool $last): string $header string $i int $encrypted string $last bool returns string throws if the chunk was changed, is not the $i-th, or not the last one when $last says it is
function encryptChunk(string $header, int $i, string $plain, bool $last): string Encrypt the local file $source into $target
function encryptFile(string $source, string $target): void $source string $target string returns void throws if one of them cannot be read or written
Encrypt what $in holds, from where it is, into $out
function encryptStream($in, $out): void The key of $filename: its 32 bytes as they are, or written in base64 or hexadecimal (head -c 32 /dev/urandom | base64)
static function FromKeyFile(string $filename): MMO\edms\helpers\Cipher $filename string returns Cipher throws if there is no such file, or it holds no key
function keyId(): string returns string what names the key, without saying anything of it: what a file encrypted with it keeps (File::$encryption )
protected function nonce(string $header, int $i): string static function PlainSize(int $size, int $chunkSize): int $size int $chunkSize int returns int the plain bytes of $size encrypted bytes, of chunks of $chunkSize
protected function readChunk($handle, int $length): string $handle $length int returns string $length bytes of $handle, fewer at its end
protected function write($handle, string $data): void function __construct(string $key) $key string throws if $key is not 32 bytes