PHPackages                             petrknap/crypto-sodium - 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. petrknap/crypto-sodium

ActiveLibrary[Security](/categories/security)

petrknap/crypto-sodium
======================

Crypto Sodium

v1.6.0(1y ago)2761[4 PRs](https://github.com/petrknap/php-crypto-sodium/pulls)1LGPL-3.0-or-laterPHPPHP &gt;=8.1

Since Apr 19Pushed 1y ago1 watchersCompare

[ Source](https://github.com/petrknap/php-crypto-sodium)[ Packagist](https://packagist.org/packages/petrknap/crypto-sodium)[ Docs](https://github.com/petrknap/php-crypto-sodium)[ Fund](https://petrknap.github.io/donate.html)[ RSS](/packages/petrknap-crypto-sodium/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (10)Dependencies (7)Versions (17)Used By (1)

Crypto Sodium
=============

[](#crypto-sodium)

A simple library that packages [functional `sodium_crypt_*`](https://www.php.net/manual/en/book.sodium.php) into objects.

Inputs and outputs are binary data, don't be afraid to [use the `petrknap/binary`](https://github.com/petrknap/php-binary).

Examples
--------

[](#examples)

### Symmetric block encryption

[](#symmetric-block-encryption)

```
use PetrKnap\CryptoSodium\SecretBox;

$secretBox = new SecretBox();
$message = 'Hello World!';
$key = $secretBox->generateKey();

$ciphertext = $secretBox->encrypt($message, $key);

echo $secretBox->decrypt($ciphertext, $key);

$secretBox->eraseData($key);
```

### Asymmetric block encryption

[](#asymmetric-block-encryption)

```
use PetrKnap\CryptoSodium\Box;

$box = new Box();
$message = 'Hello World!';
$sendersKeyPair = $box->generateKeyPair();
$recipientsKeyPair = $box->generateKeyPair();

$encryptionKeyPair = $box->generateKeyPair(
    $box->extractSecretKey($sendersKeyPair),
    $box->extractPublicKey($recipientsKeyPair),
);
$ciphertext = $box->encrypt($message, $encryptionKeyPair);
$box->eraseData($encryptionKeyPair);

$decryptionKeyPair = $box->generateKeyPair(
    $box->extractSecretKey($recipientsKeyPair),
    $box->extractPublicKey($sendersKeyPair),
);
echo $box->decrypt($ciphertext, $decryptionKeyPair);
$box->eraseData($decryptionKeyPair);

$box->eraseData($sendersKeyPair);
$box->eraseData($recipientsKeyPair);
```

### Symmetric stream encryption

[](#symmetric-stream-encryption)

```
use PetrKnap\CryptoSodium\SecretStream\XChaCha20Poly1305;

$xChaCha20Poly1305 = new XChaCha20Poly1305();
$messageChunk1 = 'Hello ';
$messageChunk2 = 'World!';
$key = $xChaCha20Poly1305->generateKey();

$pushStream = $xChaCha20Poly1305->initPush($key);
$ciphertextHeader = $pushStream->header;
$ciphertextChunk1 = $pushStream->push($messageChunk1);
$ciphertextChunk2 = $pushStream->push($messageChunk2, tag: XChaCha20Poly1305::TAG_FINAL);

$pullStream = $xChaCha20Poly1305->initPull($ciphertextHeader, $key);
echo $pullStream->pull($ciphertextChunk1);
echo $pullStream->pull($ciphertextChunk2);

$xChaCha20Poly1305->eraseData($key);
```

### Symmetric block encryption with additional data

[](#symmetric-block-encryption-with-additional-data)

```
use PetrKnap\CryptoSodium\Aead\Aes256Gcm;

$aes256Gcm = new Aes256Gcm();
$message = 'Hello World!';
$purpose = 'example';
$key = $aes256Gcm->generateKey();

$ciphertext = $aes256Gcm->encrypt($message, $key, additionalData: $purpose);

echo $aes256Gcm->decrypt($ciphertext, $key, additionalData: $purpose);

$aes256Gcm->eraseData($key);
```

### Data signing

[](#data-signing)

```
use PetrKnap\CryptoSodium\Sign;

$signer = new Sign();
$message = 'Hello World!';
$keyPair = $signer->generateKeyPair();
$secretKey = $signer->extractSecretKey($keyPair);
$publicKey = $signer->extractPublicKey($keyPair);

$signedMessage = $signer->sign($message, $secretKey);

echo $signer->verified($signedMessage, $publicKey);

$signer->eraseData($secretKey);
$signer->eraseData($keyPair);
```

---

Run `composer require petrknap/crypto-sodium` to install it. You can [support this project via donation](https://petrknap.github.io/donate.html). The project is licensed under [the terms of the `LGPL-3.0-or-later`](./COPYING.LESSER).

###  Health Score

35

—

LowBetter than 80% of packages

Maintenance49

Moderate activity, may be stable

Popularity14

Limited adoption so far

Community10

Small or concentrated contributor base

Maturity58

Maturing project, gaining track record

 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 ~41 days

Total

10

Last Release

379d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/c0e19f2ea17567eb9f536d1e00595b83630dfb8b2df6e7e422e06a9a48e85585?d=identicon)[petrknap](/maintainers/petrknap)

---

Top Contributors

[![petrknap](https://avatars.githubusercontent.com/u/8299754?v=4)](https://github.com/petrknap "petrknap (56 commits)")

---

Tags

asymmetric-cryptographyblock-ciphercryptographydecryptiondigital-signatureencryptionphpphp-librarypublic-key-cryptographysecret-key-cryptographysecuritysodiumstream-ciphersymmetric-cryptographysecuritycryptographyencryptionCurve25519aeadX25519EdDSAChaCha20public-key cryptographysecret-key cryptographyXchacha20Poly1305authenticated encryptiondigital-signaturesodiumdecryptionblock-cipherAES-256asymmetric cryptographystream ciphersymmetric cryptographyauthenticated encryption with associated data

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Code StylePHP\_CodeSniffer

Type Coverage Yes

### Embed Badge

![Health badge](/badges/petrknap-crypto-sodium/health.svg)

```
[![Health](https://phpackages.com/badges/petrknap-crypto-sodium/health.svg)](https://phpackages.com/packages/petrknap-crypto-sodium)
```

###  Alternatives

[paragonie/sodium_compat

Pure PHP implementation of libsodium; uses the PHP extension if it exists

934131.6M155](/packages/paragonie-sodium-compat)[defuse/php-encryption

Secure PHP Encryption Library

3.9k162.4M214](/packages/defuse-php-encryption)[phpseclib/phpseclib

PHP Secure Communications Library - Pure-PHP implementations of RSA, AES, SSH2, SFTP, X.509 etc.

5.6k434.8M1.3k](/packages/phpseclib-phpseclib)[paragonie/halite

High-level cryptography interface powered by libsodium

1.2k9.4M63](/packages/paragonie-halite)[simplito/elliptic-php

Fast elliptic curve cryptography

2312.2M254](/packages/simplito-elliptic-php)[nzo/url-encryptor-bundle

The NzoUrlEncryptorBundle is a Symfony Bundle used to Encrypt and Decrypt data and variables in the Web application or passed through URL

961.0M2](/packages/nzo-url-encryptor-bundle)

PHPackages © 2026

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