PHP code example of inovanti-bank / rsa-validator

1. Go to this page and download the library: Download inovanti-bank/rsa-validator library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

inovanti-bank / rsa-validator example snippets


return [
    'table_name' => 'rsa_validator', // Name of the table storing public keys
];

DB::table('rsa_validator')->insert([
    'client_id' => 'client-unique-id',
    'public_key' => '-----BEGIN PUBLIC KEY-----...-----END PUBLIC KEY-----',
]);

use InovantiBank\RSAValidator\Facades\RSAValidator;

try {
    $decryptedData = RSAValidator::decryptData('client-id', $encryptedBase64Data);
    echo $decryptedData;
} catch (\Exception $e) {
    echo 'Error: ' . $e->getMessage();
}



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;
    }
}
bash
php artisan vendor:publish --provider="InovantiBank\RSAValidator\Providers\RSAValidatorServiceProvider" --tag="config"