hxm / media-encrypt
Media Encrypt
Requires
- php: ^7.0|^8.0
- laravel/framework: ^7.0|^8.0|^9.0|^10.0
README
Media Encrypt
Media Encrypt is a powerful PHP package designed for encrypting sensitive data before storing it in a database. This package provides a secure solution to protect your critical data from unauthorized access.
Installation
You can install this package via Composer with the following command:
composer require hxm/media-encrypt
After successfully installing the Media Encrypt package, you need to perform some configuration steps to start using it in your project.
- Publish Config File: You can publish the configuration file to customize the settings according to your specific requirements by running the following command:
php artisan vendor:publish --provider="HXM\MediaEncrypt\MediaEncryptServiceProvider" --tag="media-encrypt-config"
The configuration file will be published toconfig/media-encrypt.php
. - Migrate Database: After the configuration is done, you need to run the migrate command to create the necessary tables in the database:
php artisan migrate
Usage
To use the Media Encrypt package in your Laravel project, you can refer to the following example code:
-
Inherit
CanMediaEncryptInterface
in your Model, then use theHasMediaEncrypt
trait in it.use Illuminate\Database\Eloquent\Model; use HXM\MediaEncrypt\Contracts\CanMediaEncryptInterface; use HXM\MediaEncrypt\Traits\HasMediaEncrypt; class OnlineRequest extends Model implements CanMediaEncryptInterface { use HasMediaEncrypt; #... }
-
Define: To define the
field
you want to use with theMedia Encrypt
data, you just need to add it to the$casts
attribute in the model itself. Here is the complete code for yourModel
:use Illuminate\Database\Eloquent\Model; use HXM\MediaEncrypt\Contracts\CanMediaEncryptInterface; use HXM\MediaEncrypt\Traits\HasMediaEncrypt; use HXM\MediaEncrypt\Casts\MediaEncryptCast; use HXM\MediaEncrypt\Casts\MultiMediaEncryptCast; class DemoModel extends Model implements CanMediaEncryptInterface { use HasMediaEncrypt; protected $appends = ['baseData', 'media', 'media_multi']; protected $casts = [ #... 'baseData' => MediaEncryptCast::class, 'media' => MediaEncryptCast::class, 'media_multi' => MultiMediaEncryptCast::class, ]; #... }
- The
MediaEncryptCast
will encode the data into one block. - the
MultiMediaEncryptCast
will understand that your data is stored in the form of anArray
, with each sub-element being encrypted separately. If your data is aMedia file
data array, you must use this cast
- The
-
Encrypt and store data:
$model = Demo::first(); $model->baseData = [ 'row' => 1, 'column' => 2, ]; $model->media = request()->file('file'); $model->media_multi = [ 'file1' => request()->file('file1'), 'file2' => request()->get('file2'), ]; $model->save();
-
Access the encrypted data:
$model = Demo::first(); dump($model->baseData); #@return: instance HXM\MediaEncrypt\Models\MediaEncrypt dump($model->baseData->toAray()); #@return: array:2 [ # 'row' => 1 # 'column' => 2 # ] dump($model->media->toUrl()); #@return: https://baseUrl/encrypt_media/4f03a151-14a9-4234... dump($model->media_multi->decrypt()); #@return: array:2 [ # 'file1' => https://baseUrl/encrypt_media/4f03a151-14a9-4234... # 'file2' => data:image/jpg;base64,/9j/4QAYRXh......... # ]
- When accessing the property, you will receive an instance of
HXM\MediaEncrypt\Models\MediaEncrypt
- The
Media file
data will be encrypted and stored in the database, so after decryption, we will return it to the base64 code of the content. - Raw data such as text, array will be encrypted and returned unchanged.
- When accessing the property, you will receive an instance of
-
Append: If you want to
append
the decrypted data, simply add the fields you want to the$appends
attribute of theModel
:protected $appends = ['baseData', 'media', 'media_multi'];
Contribution
If you encounter any issues or have any suggestions for improvements, please open an issue on GitHub here. We welcome contributions from the community.
License
The Media Encrypt package is distributed under the MIT License. Please refer to the LICENSE file for detailed information about this license.
Contact
If you have any questions, please feel free to contact us via email at hoanxuanmai@gmail.com. We will try to respond as soon as possible.
HoanXuanMai
We hope that the Media Encrypt package will help you secure your important data effectively. Thank you for using our package!