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

FieldCipher

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

class

FieldCipher

FieldCipher.php:32

The properties classified sante encrypted by the application before they are stored, and decrypted when they are read (ISO/IEC 27001 A.8.24): what the database, its dumps and its journal hold of them is ciphertext. Disabled by default ($enabled): nothing changes until a project turns it on, with its keys in place.

class MMO\core\helpers\FieldCipher use MMO\core\classes\tSingleton
Espace de noms
MMO\core\helpers
Utilise les traits
tSingleton
- which: [`$fields`](https://mmoframework.fr/documentation/mmo.core/php/helpers/FieldCipher.md#fieldcipher-fields), per entity class, the top-level properties to encrypt -- each must be classified `sante` (`Schema::classificationOf`), else the configuration is refused. A property encrypted can no longer be filtered, sorted, grouped nor searched by the database: list only what is read, not searched (a lesion, a comment); - how: libsodium's `sodium_crypto_secretbox` (XSalsa20-Poly1305), a random nonce per value; the value (any type) is json-encoded, and stored as one string `mmo:enc:<key id>:<base64(nonce . ciphertext)>`. null stays null; - the keys: one file per version, `{keyFolder}/fields-<id>.key` (32 random bytes, base64), outside the database and outside git; [`$key`](https://mmoframework.fr/documentation/mmo.core/php/helpers/FieldCipher.md#fieldcipher-key) names the one that encrypts, every one found decrypts (a rotation: a new id, then the values re-encrypted by the project's migration, then the old file removed); - reading decrypts whatever is encrypted, enabled or not ([`decryptFields`](https://mmoframework.fr/documentation/mmo.core/php/helpers/FieldCipher.md#fieldcipher-decryptfields)): turning it off leaves the data readable. The repository calls it (mmo.mongodb's MongoRepository: what it writes, what it reads).
// configs/config.json "MMO\\core\\helpers\\FieldCipher": {"enabled": true, "key": "1", "fields": {"Oviepro\\entities\\AccidentTravail": ["lesions"]}} FieldCipher::Shared()->encrypt(['siege' => 'Main']); // 'mmo:enc:1:...' FieldCipher::Shared()->decrypt('mmo:enc:1:...'); // ['siege' => 'Main']

Index

PREFIX
createKey
enabled
fields
key
keyFolder
keys
resolved
decrypt($value)
decryptFields($data)What a repository reads: every top-level value encrypted, decrypted (enabled or not)
encrypt($value)
encryptFields($class, $data)What a repository writes: the fields of $class encrypted, when $enabled
fieldsOf($class)
Instantiate($config) static(Re)create the shared instance immediately, configured with $config merged into the class's existing config.
IsEncrypted($value) static
IsShared() static
KeyIdOf($value) static
keyOf($id, $create)
Plain($value) static
setKey($id, $key)
Shared() staticGet the shared instance, creating (and configuring) it on first call.
Unshare($serialize) staticRemove the shared instance, so the next Shared() call creates a fresh one.

Constantes1

PREFIX

const PREFIX = 'mmo:enc:'

Propriétés7

createKey

bool $createKey = false
Type
bool

enabled

bool $enabled = false
Type
bool

fields

array $fields = []
Type
array

key

string $key = '1'
Type
string

keyFolder

string $keyFolder = '{configs}/keys'
Type
string

keys

protected array $keys = []
Type
array

resolved

protected array $resolved = []
Type
array

Méthodes14

decryptFields($data)

FieldCipher.php:163

What a repository reads: every top-level value encrypted, decrypted (enabled or not)

function decryptFields(array $data): array
$dataarray
retournearraymixed>

encryptFields($class, $data)

FieldCipher.php:150

What a repository writes: the fields of $class encrypted, when $enabled

function encryptFields(string $class, array $data): array
$classstring
$dataarray
retournearraymixed>

fieldsOf($class)

FieldCipher.php:126
function fieldsOf(string $class): array
$classstringan entity class
retournearraythe properties of $class to encrypt (its own and its parents' in $fields)
lèvefor one that is not classified sante

Instantiate($config)

statictSingleton.php:66

(Re)create the shared instance immediately, configured with $config merged into the class's existing config.

static function Instantiate(array $config = []): staticUnlike a plain [`Shared()`](https://mmoframework.fr/documentation/mmo.core/php/helpers/FieldCipher.md#fieldcipher-shared) call (which lazily builds the instance from whatever config was already loaded), this lets you supply/override config right before the instance is built.
$config = []arrayconfig values merged in before the instance is constructed
retournestaticthe newly (re)created shared instance
MyService::Instantiate(['greeting' => 'yo'])->greeting; // 'yo'

IsEncrypted($value)

staticFieldCipher.php:98
static function IsEncrypted(mixed $value): bool
$valuemixed
retourneboolwhether $value is a value this class encrypted

IsShared()

statictSingleton.php:54
static function IsShared(): bool
retournebooltrue if the shared instance has already been created (a call to Shared() would not need to construct a new one)
MyService::IsShared(); // false until the first MyService::Shared() call

keyOf($id, $create)

FieldCipher.php:70
function keyOf(string $id, bool $create = false): string
$idstring
$create = falseboolwrite it when missing
retournestringthe key of that id
lèvewhen it is missing, or invalid

Plain($value)

staticFieldCipher.php:174
static function Plain(mixed $value): mixed
$valuemixedas a repository writes it (arrays, objects of the driver)
retournemixedthe same as json holds it: an object as its array

Shared()

statictSingleton.php:22

Get the shared instance, creating (and configuring) it on first call.

static function Shared(): static
retournestatic
MyService::Shared()->greeting; // 'hello'

Unshare($serialize)

statictSingleton.php:40

Remove the shared instance, so the next Shared() call creates a fresh one.

static function Unshare(bool $serialize = true): bool
Voir
serialize) and kept as the class's config, so the next instance created picks up where this one left off; if false, the instance is simply discarded and the next one starts from the class's original config
$serialize = trueboolif true (the default) and an instance currently exists, it is serialized first (via
retournebooltrue if an instance actually existed and was removed
MyService::Unshare(); // true if MyService::Shared() had already been called; the next call rebuilds it