Download the PHP package xgrug/laravel-gm-crypto without Composer
On this page you can find all versions of the php package xgrug/laravel-gm-crypto. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Please rate this library. Is it a good library?
Informations about the package laravel-gm-crypto
laravel-gm-crypto 国密算法扩展包
Laravel 国密算法(SM3/SM4)扩展包,支持 SM3 哈希算法和 SM4 对称加密算法。
特性
- ✅ SM3 哈希算法:支持标准 SM3 和 HMAC-SM3
- ✅ SM4 对称加密:支持 ECB、CBC 等多种模式
- ✅ 灵活配置:支持通过配置文件或环境变量配置
- ✅ 多种编码:支持 Base64、十六进制等多种输出格式
- ✅ Facade 支持:提供便捷的 Facade 调用方式
- ✅ 依赖注入:支持 Laravel 依赖注入
- ✅ 驱动管理:采用 Laravel Manager 模式,易于扩展
安装
通过 Composer 安装:
发布配置文件(可选):
配置
环境变量配置
在 .env 文件中添加配置:
配置文件
配置文件位于 config/gmcrypto.php:
使用方法
SM3 哈希算法
SM4 对称加密
在控制器中使用
在 Model 中自动加密
直接使用驱动
自定义配置
API 文档
SM3 类
方法
hash(string $message): string- 计算 SM3 哈希hmac(string $message, string $key): string- 计算 HMAC-SM3verify(string $message, string $hash): bool- 验证哈希值
SM4 类
方法
encrypt(string $plaintext): string- 加密(返回二进制)decrypt(string $ciphertext): string- 解密encryptBase64(string $plaintext): string- 加密并返回 Base64decryptBase64(string $ciphertext): string- 解密 Base64 字符串encryptHex(string $plaintext): string- 加密并返回十六进制decryptHex(string $ciphertext): string- 解密十六进制字符串
支持的加密模式
ECB- 电子密码本模式CBC- 密码块链接模式(推荐)
支持的填充方式
pkcs7- PKCS#7 填充(推荐)zero- 零填充
实际应用场景
1. 敏感数据加密存储
2. 数据完整性校验
3. API 接口签名
安全建议
- 密钥管理:不要在代码中硬编码密钥,使用环境变量存储
- 定期更换密钥:建议定期更换加密密钥
- 使用 CBC 模式:相比 ECB 模式更安全
- HTTPS 传输:加密数据在传输时仍需使用 HTTPS
- 密钥长度:SM4 密钥必须为 128 位(32 位十六进制字符)
依赖要求
- PHP >= 8.2
- Laravel >= 10
- OpenSSL 扩展
性能优化
- SM3/SM4 算法采用纯 PHP 实现,性能略低于原生扩展
- 建议在高并发场景下使用缓存减少重复计算
- 考虑使用 C 扩展(如 OpenSSL)以获得更好性能
测试
贡献
欢迎提交 Pull Request 或报告问题。
许可证
MIT License
相关资源
更新日志
v1.0.0 (2024-12-10)
- 初始版本发布
- 支持 SM3 哈希算法
- 支持 SM4 对称加密算法
- 支持 ECB、CBC 模式
- 提供 Facade 和依赖注入支持
All versions of laravel-gm-crypto with dependencies
PHP Build Version
Package Version
Requires
php Version
^8.0|^8.1|^8.2
illuminate/support Version ^10.0|^11.0|^12.0
ext-openssl Version *
illuminate/support Version ^10.0|^11.0|^12.0
ext-openssl Version *
The package xgrug/laravel-gm-crypto contains the following files
Loading the files please wait ...