Skip to content

Commit

Permalink
Merge pull request #2 from TapanDerasari/existing-data-encryption-san…
Browse files Browse the repository at this point in the history
…deep-04-Jan-2024

Existing data encryption
  • Loading branch information
TapanDerasari authored Jan 4, 2024
2 parents 8beb158 + dccf402 commit a2f6704
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 0 deletions.
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# Laravel/Lumen MySql AES Encrypt/Decrypt

<a href="https://packagist.org/packages/tapanderasari/laravel-mysql-encrypt"><img src="https://img.shields.io/packagist/dt/tapanderasari/laravel-mysql-encrypt" alt="Total Downloads"></a> <a href="https://img.shields.io/packagist/v/tapanderasari/laravel-mysql-encrypt"><img src="https://img.shields.io/packagist/v/tapanderasari/laravel-mysql-encrypt" alt="Latest Stable Version"></a> <a href="https://github.com/TapanDerasari/laravel-mysql-encrypt/blob/master/LICENSE"><img src="https://img.shields.io/packagist/l/tapanderasari/laravel-mysql-encrypt" alt="License"></a>

Laravel/Lumen database encryption at database side using native AES_DECRYPT and AES_ENCRYPT functions.
Automatically encrypt and decrypt fields in your Models.

Expand Down Expand Up @@ -100,6 +102,20 @@ DB::statement('ALTER TABLE `users` ADD `last_name` VARBINARY(300)');
DB::statement('ALTER TABLE `users` ADD `email` VARBINARY(300)');
DB::statement('ALTER TABLE `users` ADD `telephone` VARBINARY(50)');
```
## Implementing encryption for existing data

For this you can create one command like

```
php artisan make:command EncryptionForExistingData
```

In this command you fetch existing table or model data without global scope `DecryptSelectScope`.

You can refer the example, clicking on below Example button:

<a href="https://github.com/TapanDerasari/laravel-mysql-encrypt/blob/master/existing_data_encryption.md" target="new"><img src="https://img.shields.io/badge/Example-green"></a>


## License

Expand Down
63 changes: 63 additions & 0 deletions existing_data_encryption.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@

## Example of existing data encryption

```php
<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use TapanDerasari\MysqlEncrypt\Scopes\DecryptSelectScope;


class EncryptionForExistingData extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'encryption-for-existing-data';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Encryption for existing data based on passed model with encryptable fields';

/**
* Execute the console command.
*/
public function handle(): void
{

// Put existing models here in which you want to apply encryption
$modelList = ['User'];
foreach ($modelList as $model) {
$modelName = 'App\\Models\\' . $model;

// Check if the model exists
if (!class_exists($modelName)) {
$this->error("Model '{$modelName}' not found!");

return;
}

// Instantiate the model
$modelObj = new $modelName;
$modelData = $modelObj->withoutGlobalScopes([DecryptSelectScope::class])->get();

foreach ($modelData as $modelRecord) {
$updateModel = $modelName::findOrFail($modelRecord->id);
$fields = $updateModel->encryptable;
foreach ($fields as $field) {
$updateModel->$field = $modelRecord->$field;
}
$updateModel->save();
}
}

}
}
```

0 comments on commit a2f6704

Please sign in to comment.