bariew/whatsapp-crypto
Composer 安装命令:
composer require bariew/whatsapp-crypto
包简介
PSR-7 stream decorators for WhatsApp-style encryption and decryption
README 文档
README
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
- 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
Install the package via Composer:
composer require bariew/whatsapp-crypto
Requirements
- PHP 7.4 or higher
ext-opensslextensionext-hashextensionpsr/http-message(^1.0 or ^2.0)guzzlehttp/psr7(^2.0)
Quick Start
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
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
Run the test suite:
composer test
The project includes integration tests with sample files for AUDIO, IMAGE, and VIDEO media types.
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 encryptionVIDEO.sidecar- Example sidecar file for chunk-based verification
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
This package is open-source software licensed under the MIT license.
Credits
This implementation is based on the WhatsApp encryption protocol as documented in various reverse-engineering efforts and the Signal protocol specifications.
bariew/whatsapp-crypto 适用场景与选型建议
bariew/whatsapp-crypto 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 0 次下载、GitHub Stars 达 0, 最近一次更新时间为 2026 年 02 月 09 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 bariew/whatsapp-crypto 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 bariew/whatsapp-crypto 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 0
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 40
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2026-02-09