inovanti-bank/rsa-validator
Composer 安装命令:
composer require inovanti-bank/rsa-validator
包简介
Package for validation and decryption using RSA public keys (Private Key and Public Key)
README 文档
README
A Laravel ^11 package for validating and decrypting data using RSA public and private keys. This package is designed to simplify secure communication with APIs using client-specific keys.
Features
- Decrypts and validates Base64-encoded data using public RSA keys.
- Supports configuration of key storage in the database.
- Automatically loads migrations and configuration for Laravel projects.
- Fully compatible with Laravel ^11 and PHP ^8.0+.
Installation
To install the package via Composer, run:
composer require inovanti-bank/rsa-validator
Configuration
- After installing the package, the configuration and migrations are automatically loaded.
- If you need to customize the configuration file, you can publish it manually using:
php artisan vendor:publish --provider="InovantiBank\RSAValidator\Providers\RSAValidatorServiceProvider" --tag="config"
- This will publish the config/rsa-validator.php file. The default configuration is as follows:
return [ 'table_name' => 'rsa_validator', // Name of the table storing public keys ];
How to Use
Storing Public Keys
Ensure you have a table (rsa_validator by default) to store the public keys. Use the following schema to insert a public key:
DB::table('rsa_validator')->insert([ 'client_id' => 'client-unique-id', 'public_key' => '-----BEGIN PUBLIC KEY-----...-----END PUBLIC KEY-----', ]);
Decrypting Data
To decrypt and validate incoming data:
use InovantiBank\RSAValidator\Facades\RSAValidator; try { $decryptedData = RSAValidator::decryptData('client-id', $encryptedBase64Data); echo $decryptedData; } catch (\Exception $e) { echo 'Error: ' . $e->getMessage(); }
Exception Handling
The following exceptions may be thrown:
- PublicKeyNotFoundException: Public key for the specified client is not found.
- DecryptionFailedException: The decryption process faile
Testing
It is highly recommended that you create some tests in your Laravel project to ensure that it works, see some test examples:
<?php namespace Tests\Unit; use DB; use Illuminate\Foundation\Testing\RefreshDatabase; use InovantiBank\RSAValidator\Exceptions\PublicKeyNotFoundException; use InovantiBank\RSAValidator\Services\RSAValidatorService; use InvalidArgumentException; use Tests\TestCase; class RSAValidatorServiceTest extends TestCase { use RefreshDatabase; public function test_decrypt_data_successfully() { DB::table('rsa_validator')->insert([ 'client_id' => 'valid-client', 'public_key' => file_get_contents(__DIR__.'/mock_public_key.pem'), ]); $publicKeyPath = __DIR__.'/mock_public_key.pem'; $this->assertFileExists($publicKeyPath); $publicKey = file_get_contents($publicKeyPath); $this->assertNotEmpty($publicKey); $originalData = 'mock-data'; $encryptedData = null; $result = openssl_public_encrypt($originalData, $encryptedData, $publicKey); $this->assertTrue($result, 'Falha ao criptografar os dados'); $encodedEncryptedData = base64_encode($encryptedData); $this->assertNotEmpty($encodedEncryptedData); } public function test_invalid_public_key_throws_exception() { $service = app(RSAValidatorService::class); $this->expectException(PublicKeyNotFoundException::class); $service->decryptData('invalid-client', base64_encode('fake-encrypted-data')); } public function test_invalid_table_name_configuration_throws_exception() { config()->set('rsa-validator.table_name', ''); $this->expectException(InvalidArgumentException::class); $this->expectExceptionMessage('The "table_name" configuration in rsa-validator.php must be a non-empty string.'); new RSAValidatorService; } public function test_missing_table_throws_exception() { config()->set('rsa-validator.table_name', 'non_existing_table'); DB::shouldReceive('getSchemaBuilder->hasTable') ->once() ->with('non_existing_table') ->andReturn(false); $this->expectException(InvalidArgumentException::class); $this->expectExceptionMessage('The table \'non_existing_table\' does not exist in the database. Please run migrations.'); new RSAValidatorService; } }
Contributing
If you want to contribute to this package:
- Fork the repository.
- Create a new branch.
- Submit a pull request.
License
This package is open-source software licensed under the MIT license.
inovanti-bank/rsa-validator 适用场景与选型建议
inovanti-bank/rsa-validator 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.21k 次下载、GitHub Stars 达 1, 最近一次更新时间为 2024 年 12 月 11 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 inovanti-bank/rsa-validator 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 inovanti-bank/rsa-validator 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 1.21k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2024-12-11