tigr/laravel-compact-encrypter
Composer 安装命令:
composer require tigr/laravel-compact-encrypter
包简介
Encrypter for Laravel that produces much shorter output
README 文档
README
Drop-in replacement for Laravel encrypter that produces much more concise and URL-safe output with fallback for encrypted data in old format. For Laravel 5.5+.
Installation
composer require tigr/laravel-compact-encrypter
Then Laravel should do the magic and register our service provider.
Why?
Laravel's encrypter is very inefficient when it comes to space usage. It takes 64 bytes of raw encrypted data and converts it into 192 characters. How?
Laravel stores encrypted data in base64-encoded JSON containing base64 and HEX strings inside.
So, base64_encode(json_encode(base64_encode(data) + sha256 hex hash)). Just compare encryption
of simple string "hello":
# Laravel Encrypter: 192 characters long
$ artisan tinker
>>> Crypt::encryptString('hello')
=> "eyJpdiI6IlZRZFY5TVZiZVdNSlg3RVdDYTNDbHc9PSIsInZhbHVlIjoiNk9rXC9oV3hZaWEzNE95SU5xMUVHUWc9PSIsIm1hYyI6ImIyZDQ1MTdiODlhMzU1ZjQ1NmU3N2ZlN2I4OGU0Yzc2MjIyZDBkMzAwMGViNjM2OTFlMTZkOGY4MDFjYTg1NDIifQ=="
# add Compact Encrypter
$ composer require tigr/laravel-compact-encrypter
# Compact Encrypter: 70 characters long
$ artisan tinker
>>> Crypt::encryptString('hello')
=> "ABvcrldH1QNn8AgEL_LY-E_Cj04MRWPn3M-kSuvf3DAjzsNfFqC_lUml6iqGkRTjlox2kA"
# Compact Encrypter dropping verification hash: 53 characters long
>>> Crypt::encryptString('hello', false)
=> "z4zztewR8vQ7QId_P6diDRW2DvVPhwf4xh8gNss4G1o"
Who cares?
First, if you have some encrypted cookies, you might easily run into 400 errors with "header too long" from your server, or from load-balancing/DDoS-protection proxies in the middle. On average, Compact Encrypter produces 25-275% more compact output or up to 360% more compact with hash dropped.
Second, you might want to use self-contained tokens in URLs (no need for token DB), but guess what, 192 characters some of which are URL-escaped are way too ugly.
How does it work?
- No intermediate base64 or hex encoding, all data is raw binary.
- No JSON, use pack()/unpack().
- Use URL-safe version of base64 (drop trailing '=', replace '/+' with '-_').
- For hashing, use SHA1 instead of SHA256. I know, I know, but for real-world purposes SHA1 is still good enough.
- Allow dropping Mac (validation hash) whatsoever in case you want it really short.
- And provide fallback decryption if we ever encounter data encrypted with Laravel Encryptor
tigr/laravel-compact-encrypter 适用场景与选型建议
tigr/laravel-compact-encrypter 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 26 次下载、GitHub Stars 达 1, 最近一次更新时间为 2019 年 05 月 14 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 tigr/laravel-compact-encrypter 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 tigr/laravel-compact-encrypter 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 26
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 8
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2019-05-14