PHPackages                             friendsofhyperf/encryption - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Security](/categories/security)
4. /
5. friendsofhyperf/encryption

ActiveLibrary[Security](/categories/security)

friendsofhyperf/encryption
==========================

The encryption component for Hyperf.

v3.2.1(3w ago)314.2k↓29.5%1MITPHP

Since May 21Pushed 2w ago1 watchersCompare

[ Source](https://github.com/friendsofhyperf/encryption)[ Packagist](https://packagist.org/packages/friendsofhyperf/encryption)[ Fund](https://hdj.me/sponsors/)[ GitHub Sponsors](https://github.com/huangdijia)[ RSS](/packages/friendsofhyperf-encryption/feed)WikiDiscussions main Synced yesterday

READMEChangelog (10)Dependencies (12)Versions (110)Used By (1)

Encryption
==========

[](#encryption)

[中文说明](README_CN.md)

The encryption component provides authenticated encryption for values and strings in Hyperf.

Installation
------------

[](#installation)

```
composer require friendsofhyperf/encryption
```

The package targets Hyperf 3.2. Install the optional `opis/closure` package only when encrypted closures need to be signed:

```
composer require opis/closure
```

Configuration
-------------

[](#configuration)

Publish the configuration file:

```
php bin/hyperf.php vendor:publish friendsofhyperf/encryption
```

This creates `config/autoload/encryption.php` with these settings:

Configuration keyEnvironment variableDefault`encryption.key``APP_KEY`A bundled example `base64:` key`encryption.cipher``APP_CIPHER``AES-256-CBC`Replace the bundled example key before using the component. A `base64:` prefix tells the component to Base64-decode the remaining value before validating its length. Generate a new key for the default cipher with:

```
php -r "echo 'base64:'.base64_encode(random_bytes(32)).PHP_EOL;"
```

Supported ciphers and raw key lengths are:

CipherKey length`AES-128-CBC`16 bytes`AES-256-CBC`32 bytes`AES-128-GCM`16 bytes`AES-256-GCM`32 bytesCipher names are case-insensitive. A missing key throws `FriendsOfHyperf\Encryption\Exception\MissingKeyException`; an unsupported cipher or incorrect key length throws `RuntimeException`.

Helper Functions
----------------

[](#helper-functions)

The package autoloads namespaced `encrypt()` and `decrypt()` functions. Import them before use:

```
use function FriendsOfHyperf\Encryption\decrypt;
use function FriendsOfHyperf\Encryption\encrypt;

$payload = encrypt(['id' => 1]);
$value = decrypt($payload);
```

`encrypt(mixed $value, bool $serialize = true)` serializes values by default, and `decrypt(string $payload, bool $unserialize = true)` reverses that behavior. Pass `false` to both second arguments when handling a raw string without serialization.

Encrypter API
-------------

[](#encrypter-api)

The container binds `FriendsOfHyperf\Encryption\Encrypter`, `FriendsOfHyperf\Encryption\Contract\Encrypter`, and `FriendsOfHyperf\Encryption\Contract\StringEncrypter` to the configured encrypter.

```
use FriendsOfHyperf\Encryption\Contract\StringEncrypter;

class TokenService
{
    public function __construct(private StringEncrypter $encrypter)
    {
    }

    public function encrypt(string $token): string
    {
        return $this->encrypter->encryptString($token);
    }
}
```

The concrete `Encrypter` also exposes `encrypt()`, `decrypt()`, `getKey()`, `getAllKeys()`, `getPreviousKeys()`, `previousKeys()`, and the static `supported()` and `generateKey()` methods.

Key Rotation
------------

[](#key-rotation)

Configure previous raw keys on the concrete encrypter to decrypt existing payloads after rotating the current key. New payloads always use the current key.

```
use FriendsOfHyperf\Encryption\Encrypter;

$encrypter->previousKeys([
    base64_decode('PREVIOUS_BASE64_KEY'),
]);
```

Every previous key must have the correct length for the current cipher.

Failures and Optional Closure Signing
-------------------------------------

[](#failures-and-optional-closure-signing)

Encryption failures throw `FriendsOfHyperf\Encryption\Contract\EncryptException`. Invalid, tampered, or undecryptable payloads throw `FriendsOfHyperf\Encryption\Contract\DecryptException`.

When `opis/closure` is installed and `encryption.key` is configured, the boot listener registers the same parsed key with `Opis\Closure\SerializableClosure` for closure signing. The component does not require `opis/closure` for normal value or string encryption.

###  Health Score

53

—

FairBetter than 96% of packages

Maintenance96

Actively maintained with recent releases

Popularity28

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity65

Established project with proven stability

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~14 days

Recently: every ~48 days

Total

109

Last Release

24d ago

Major Versions

v2.0.9 → v3.0.0-beta122022-05-26

v2.0.10 → v3.0.0-beta242022-06-24

v2.0.19 → v3.0.0-rc.162022-09-21

v2.0.24 → v3.0.0-rc.462022-12-14

v2.0.28 → v3.0.142023-02-10

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/8337659?v=4)[Deeka Wong](/maintainers/huangdijia)[@huangdijia](https://github.com/huangdijia)

---

Top Contributors

[![huangdijia](https://avatars.githubusercontent.com/u/8337659?v=4)](https://github.com/huangdijia "huangdijia (72 commits)")

---

Tags

phpencryptionswoolehyperfv3.2

### Embed Badge

![Health badge](/badges/friendsofhyperf-encryption/health.svg)

```
[![Health](https://phpackages.com/badges/friendsofhyperf-encryption/health.svg)](https://phpackages.com/packages/friendsofhyperf-encryption)
```

###  Alternatives

[hyperf/http-server

A HTTP Server for Hyperf.

103.0M371](/packages/hyperf-http-server)[hyperf/database

A flexible database library.

192.9M321](/packages/hyperf-database)[hyperf/crontab

A crontab component for Hyperf.

131.7M77](/packages/hyperf-crontab)[friendsofhyperf/sentry

The sentry component for Hyperf.

1974.5k](/packages/friendsofhyperf-sentry)[hyperf/swagger

A swagger library for Hyperf.

20371.1k7](/packages/hyperf-swagger)[hyperf/database-pgsql

A pgsql handler for hyperf/database.

12321.2k21](/packages/hyperf-database-pgsql)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
