Download the PHP package tuzelko/yii2-encrypted-attribute without Composer

On this page you can find all versions of the php package tuzelko/yii2-encrypted-attribute. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package yii2-encrypted-attribute

Yii2 Encrypted Attribute extension

Project Status: Active Tests Latest Version PHP Version Total Downloads License

Transparent at-rest encryption for Yii2 ActiveRecord attributes with pluggable ciphers (libsodium secretbox or AES-256-GCM via openssl, the choice is always explicit).

The physical attribute always holds ciphertext; plain values are accessed through a virtual {attribute}_decrypted sibling. The model can therefore be serialized to cache, logged, or dumped without ever exposing plaintext — and the encryption key is never stored on the model or the behavior.

Features

Requirements

Installation

Quick start

1. Register a key provider in the DI container

Generate a key:

2. Attach the behavior

The encrypted column must be a TEXT/VARCHAR (the stored value is base64).

3. Use the virtual accessors

Configuration

Property Type Default Description
attributes array [] Physical column names to virtualize
keyName string (required) Key name resolved through KeyProviderInterface::getRaw()
suffix string '_decrypted' Suffix of the virtual accessor
cipher CipherInterface\|class-string (required) Encryption algorithm (see Ciphers)
cipherOptions array [] Static per-call cipher options, keyed by the cipher's OPTION_* constants (see Associated data)
cipherOptionMethods array [] Dynamic per-call cipher options: option key => owner method name, called with the attribute name per operation

Ciphers

There is intentionally no default cipher — the algorithm a model uses must be visible in its configuration.

Cipher Algorithm Requires Key Stored format
SodiumSecretboxCipher XSalsa20-Poly1305 ext-sodium 32 bytes base64(nonce[24] \|\| ciphertext)
XChaCha20Poly1305Cipher XChaCha20-Poly1305 (IETF) ext-sodium 32 bytes base64(nonce[24] \|\| ciphertext)
AesGcmCipher AES-256-GCM ext-openssl 32 bytes base64(nonce[12] \|\| tag[16] \|\| ciphertext)

All three are authenticated (AEAD) and use a fresh random nonce per write; nonce sizes are collision-safe for random generation. Algorithms with short nonces unsafe for random use (ChaCha20-Poly1305 IETF) or without built-in authentication (AES-CBC) are intentionally not shipped.

The extension check happens lazily, when the cipher is first used — environments without ext-sodium can install the package and use AesGcmCipher freely.

A custom algorithm is a class implementing CipherInterface (encrypt/decrypt, authenticated encryption with a fresh random nonce per call). Extend AbstractCipher to get strict option validation for free.

Associated data and cipher options

Each cipher declares the per-call options it understands as its own OPTION_* class constants; unknown options are rejected with an exception, never silently ignored. The AEAD ciphers (XChaCha20Poly1305Cipher, AesGcmCipher) support associated data — authenticated but unencrypted context the ciphertext is cryptographically bound to. SodiumSecretboxCipher predates the AEAD interface and supports no options.

Binding a value to its column prevents transplanting ciphertext between columns or tables (an attacker with DB write access cannot swap one valid encrypted value for another).

Options come in two flavors: static literals in cipherOptions, and dynamic values in cipherOptionMethods — owner method names (validator-style), called on every operation with the physical attribute name:

Closures are deliberately not supported (rejected at init): behaviors are serialized together with their owner, so a Closure in the configuration would make the model uncacheable — serialize($model) throws. Method names are plain strings and keep the owner fully serializable.

The same options must reproduce on decrypt, so derive them only from immutable context:

Note: the stored value does not identify the cipher that produced it. The cipher choice is deliberately explicit configuration, never auto-detected from the environment — otherwise the same model could silently encrypt differently on different hosts. Switching ciphers requires re-encrypting existing rows (same procedure as key rotation).

How it works

Why the key cannot be passed directly

The behavior deliberately accepts only a key name — there is no key property and never will be. This is not an inconvenience, it is the security model:

In Yii2, behaviors are serialized together with their owner. If the raw key (or anything that memoizes it) lived on the behavior, then every serialize($model) — caching an AR model in Redis/Valkey, storing it in a session, queueing it in a job payload — would write the encryption key right next to the ciphertext it protects. One KEYS * away from a full decrypt.

With the key-storage indirection the serialized model carries only the key name (a meaningless string), while the actual bytes live in the tuzelko/yii2-key-storage component inside the DI container and are resolved at encrypt/decrypt time. The test suite asserts this invariant: a serialized model contains neither plaintext nor key material.

Searching and indexing

Ciphertexts are randomized, so encrypted columns cannot be searched, compared, or indexed by plaintext. Keep values that must be queryable out of attributes, or store a separate deterministic digest (e.g. HMAC) alongside for lookups.

Key rotation

The behavior intentionally has no multi-key fallback. To rotate a key: add the new key under a new name, re-encrypt existing rows (read with old keyName → write with new keyName), then drop the old key. Doing this in a console migration keeps the window where both keys exist explicit and short.

Error handling

Condition Result
keyName or cipher not configured yii\base\InvalidConfigException on behavior init
Key unknown / malformed / wrong length tuzelko\yii\keystorage\InvalidKeyException
Tampered or corrupted ciphertext, wrong key or associated data RuntimeException (authentication tag mismatch)
Unknown / unsupported / invalid cipher option InvalidArgumentException

Running tests

Tests run inside Docker (PHP 8.3 + SQLite) with no local setup required.

License

MIT — see LICENSE.


All versions of yii2-encrypted-attribute with dependencies

PHP Build Version
Package Version
Requires php Version >=8.0
yiisoft/yii2 Version ~2.0
tuzelko/yii2-key-storage Version ^1.0
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package tuzelko/yii2-encrypted-attribute contains the following files

Loading the files please wait ...