MMOframework
Pourquoi l'IA Playground Bibliothèques Développeurs Agence
FR EN
Créer un projet en direct
Index

Cipher

MMO\edms\helpers · 1 symbole · générée le 30 Nov 2026

class

Cipher

Cipher.php:23

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
Espace de noms
MMO\edms\helpers
What 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/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

Constantes4

CHUNK_SIZE

const CHUNK_SIZE = 65536

HEADER_SIZE

const HEADER_SIZE = 28

MAGIC

const MAGIC = 'MMOEDMS1'

TAG_SIZE

const TAG_SIZE = 16

Propriétés1

key

protected string $key
Type
string

Méthodes14

additionalData($header, $i, $last)

Cipher.php:144
protected function additionalData(string $header, int $i, bool $last): string
$headerstring
$iint
$lastbool
retournestring

decrypt($handle)

Cipher.php:99
function decrypt($handle)
$handlethe encrypted bytes (a storage's), the stream returned closes it
retournea stream of the plain bytes: it reads, seeks, and its fstat() gives their size
lèveif $handle holds no bytes this class wrote

decryptChunk($header, $i, $encrypted, $last)

Cipher.php:134
function decryptChunk(string $header, int $i, string $encrypted, bool $last): string
$headerstring
$iint
$encryptedstring
$lastbool
retournestring
lèveif the chunk was changed, is not the $i-th, or not the last one when $last says it is

encryptFile($source, $target)

Cipher.php:64

Encrypt the local file $source into $target

function encryptFile(string $source, string $target): void
$sourcestring
$targetstring
retournevoid
lèveif one of them cannot be read or written

encryptStream($in, $out)

Cipher.php:80

Encrypt what $in holds, from where it is, into $out

function encryptStream($in, $out): void
$in
$out
retournevoid

FromKeyFile($filename)

staticCipher.php:44

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
$filenamestring
retourneCipher
lèveif there is no such file, or it holds no key

PlainSize($size, $chunkSize)

staticCipher.php:122
static function PlainSize(int $size, int $chunkSize): int
$sizeint
$chunkSizeint
retourneintthe plain bytes of $size encrypted bytes, of chunks of $chunkSize

readChunk($handle, $length)

Cipher.php:152
protected function readChunk($handle, int $length): string
$handle
$lengthint
retournestring$length bytes of $handle, fewer at its end

readHeader($handle)

Cipher.php:112
function readHeader($handle): array
$handlethe encrypted bytes, at their start
retournearrayint, header: string} what the header of $handle says
lèveif it is not one of this class's

__construct($key)

Cipher.php:35
function __construct(string $key)
$keystring
lèveif $key is not 32 bytes