PHPackages                             bariew/whatsapp-crypto - 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. [Utility &amp; Helpers](/categories/utility)
4. /
5. bariew/whatsapp-crypto

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

bariew/whatsapp-crypto
======================

PSR-7 stream decorators for WhatsApp-style encryption and decryption

0.1.1(4mo ago)00MITPHPPHP ^7.4|^8.0

Since Feb 9Pushed 4mo agoCompare

[ Source](https://github.com/bariew/whatsapp-crypto)[ Packagist](https://packagist.org/packages/bariew/whatsapp-crypto)[ RSS](/packages/bariew-whatsapp-crypto/feed)WikiDiscussions main Synced today

READMEChangelogDependencies (3)Versions (3)Used By (0)

WhatsApp Crypto
===============

[](#whatsapp-crypto)

[![Latest Version](https://camo.githubusercontent.com/6ce08f5ba76fa65563192b807fe4dedc7faaaa0a06470d42a154088f019524e1/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6261726965772f77686174736170702d63727970746f2e737667)](https://packagist.org/packages/bariew/whatsapp-crypto)[![License](https://camo.githubusercontent.com/7013272bd27ece47364536a221edb554cd69683b68a46fc0ee96881174c4214c/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d626c75652e737667)](https://opensource.org/licenses/MIT)[![PHP Version](https://camo.githubusercontent.com/92796e239b27a6efdd63fea6f14a269cf8e109cdeee963eb967646961705e919/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f7068702d253545372e34253743253545382e302d3838393242462e737667)](https://packagist.org/packages/bariew/whatsapp-crypto)

PSR-7 stream decorators for WhatsApp-style encryption and decryption of media files. This library implements the same encryption algorithm used by WhatsApp to encrypt images, videos, audio, and documents.

Features
--------

[](#features)

- **Stream-based encryption/decryption**: Uses PSR-7 stream decorators for memory-efficient processing
- **WhatsApp-compatible**: Implements the exact same encryption algorithm as WhatsApp
- **Chunk-based MAC generation**: Supports sidecar file generation for streaming scenarios
- **No external dependencies**: Only requires PHP extensions that are commonly available
- **Well-tested**: Comprehensive unit and integration tests with sample files

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

[](#installation)

Install the package via Composer:

```
composer require bariew/whatsapp-crypto
```

Requirements
------------

[](#requirements)

- PHP 7.4 or higher
- `ext-openssl` extension
- `ext-hash` extension
- `psr/http-message` (^1.0 or ^2.0)
- `guzzlehttp/psr7` (^2.0)

Quick Start
-----------

[](#quick-start)

### Encrypting a File

[](#encrypting-a-file)

```
use GuzzleHttp\Psr7\Stream;
use bariew\WhatsAppCrypto\Factory\EncryptedStreamFactory;
use bariew\WhatsAppCrypto\Enum\MediaType;

// Your 32-byte mediaKey (raw binary)
$mediaKey = random_bytes(32);

// Open the source file and encrypted output
$sourceStream = fopen('/path/to/original/file.jpg', 'rb');
$encryptedStream = fopen('/path/to/encrypted/file.encrypted', 'wb');

$encryptedPsr7Stream = new Stream($encryptedStream);

// Create encrypted stream
$encryptor = new EncryptedStreamFactory();
$encrypted = $encryptor->create($encryptedPsr7Stream, $mediaKey, MediaType::IMAGE);

// Encrypt the file
$encrypted->readAndWrite(new Stream($sourceStream));
```

### Decrypting a File

[](#decrypting-a-file)

```
use GuzzleHttp\Psr7\Stream;
use bariew\WhatsAppCrypto\Factory\DecryptedStreamFactory;
use bariew\WhatsAppCrypto\Enum\MediaType;

// Your 32-byte mediaKey (raw binary)
$mediaKey = file_get_contents('/path/to/file.key');
$decryptedStream = fopen('/path/to/decrypted/file.jpg', 'wb');

// Create decrypted stream
$decryptor = new DecryptedStreamFactory();
$decrypted = $decryptor->createFromFile('/path/to/encrypted/file.encrypted', $mediaKey, MediaType::IMAGE);

// Decrypt the file
while (!$decrypted->eof()) {
    fwrite($decryptedStream, $decrypted->read(8192));
}
$decrypted->close();
```

Testing
-------

[](#testing)

Run the test suite:

```
composer test
```

The project includes integration tests with sample files for AUDIO, IMAGE, and VIDEO media types.

### Sample Files

[](#sample-files)

The `samples/` directory contains test files:

- `*.original` - Original unencrypted files
- `*.encrypted` - Encrypted versions using WhatsApp-compatible encryption
- `*.key` - The 32-byte mediaKeys used for encryption
- `VIDEO.sidecar` - Example sidecar file for chunk-based verification

Security Considerations
-----------------------

[](#security-considerations)

- **MAC Truncation**: WhatsApp uses the first 10 bytes of HMAC-SHA256. This is a deliberate design choice for compactness, though it reduces the theoretical security margin.
- **Timing-Safe Comparison**: The library uses `hash_equals()` for MAC verification to prevent timing attacks.
- **Key Management**: Keep your mediaKeys secure. Anyone with the mediaKey can decrypt the corresponding media.

License
-------

[](#license)

This package is open-source software licensed under the [MIT license](https://opensource.org/licenses/MIT).

Credits
-------

[](#credits)

This implementation is based on the WhatsApp encryption protocol as documented in various reverse-engineering efforts and the Signal protocol specifications.

###  Health Score

29

—

LowBetter than 57% of packages

Maintenance74

Regular maintenance activity

Popularity0

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity31

Early-stage or recently created project

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

Total

2

Last Release

145d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/8214f36975034f4134506744cd956e8415491006c1b3512af22bf75f91165f06?d=identicon)[bariew](/maintainers/bariew)

---

Top Contributors

[![bariew](https://avatars.githubusercontent.com/u/827508?v=4)](https://github.com/bariew "bariew (2 commits)")

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/bariew-whatsapp-crypto/health.svg)

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

###  Alternatives

[aws/aws-sdk-php

AWS SDK for PHP - Use Amazon Web Services in your PHP project

6.3k543.5M2.6k](/packages/aws-aws-sdk-php)[google/cloud-core

Google Cloud PHP shared dependency, providing functionality useful to all components.

346132.9M112](/packages/google-cloud-core)[algolia/algoliasearch-client-php

API powering the features of Algolia.

69735.1M159](/packages/algolia-algoliasearch-client-php)[oat-sa/tao-core

TAO core extension

66143.7k124](/packages/oat-sa-tao-core)[sylius/sylius

E-Commerce platform for PHP, based on Symfony framework.

8.5k5.9M738](/packages/sylius-sylius)[gotenberg/gotenberg-php

A PHP client for interacting with Gotenberg, a developer-friendly API for converting numerous document formats into PDF files, and more!

3856.2M31](/packages/gotenberg-gotenberg-php)

PHPackages © 2026

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