-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Add eloquent builder support - Add datetime support, refactor casts - Update encryption to always have the same result for the same key + string - Add tests - Update README
- Loading branch information
Showing
18 changed files
with
447 additions
and
58 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,24 @@ | ||
<?php | ||
|
||
return [ | ||
'laravel_encryption_enabled' => env('LARAVEL_ENCRYPTION_ENABLED', true), | ||
/** | ||
* Enable or disable the encryption. | ||
*/ | ||
'enabled' => env('LARAVEL_ENCRYPTION_ENABLED', true), | ||
|
||
/** | ||
* The encryption key. | ||
* | ||
* Default: your app key. | ||
*/ | ||
'key' => env('LARAVEL_ENCRYPTION_KEY', null), | ||
|
||
/** | ||
* The encryption cipher. | ||
* | ||
* Supports any cipher method supported by openssl_get_cipher_methods(). | ||
* | ||
* Default: AES-256-CBC. | ||
*/ | ||
'cipher' => env('LARAVEL_ENCRYPTION_CIPHER', 'AES-256-CBC'), | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<?php | ||
|
||
namespace Joelwmale\LaravelEncryption\Builders; | ||
|
||
use Illuminate\Database\Eloquent\Builder; | ||
|
||
class EloquentBuilder extends Builder | ||
{ | ||
use OrderByEncrypted; | ||
use WhereEncrypted; | ||
use WhereInEncrypted; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?php | ||
|
||
namespace Joelwmale\LaravelEncryption\Builders; | ||
|
||
trait OrderByEncrypted | ||
{ | ||
public function orderByEncrypted($column, $direction = 'desc') | ||
{ | ||
$results = $this->get(); | ||
|
||
$results = $results->sortBy(function ($result) use ($column) { | ||
return $result->$column; | ||
}, SORT_REGULAR, $direction === 'desc'); | ||
|
||
return $results; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?php | ||
|
||
namespace Joelwmale\LaravelEncryption\Builders; | ||
|
||
use Joelwmale\LaravelEncryption\Services\EncryptService; | ||
|
||
trait WhereEncrypted | ||
{ | ||
public function whereEncrypted($column, $value = null, $operator = '=') | ||
{ | ||
$encryptedValue = EncryptService::encrypt($value); | ||
|
||
return self::where($column, $operator, $encryptedValue); | ||
} | ||
|
||
public function orWhereEncrypted($column, $value = null, $operator = '=') | ||
{ | ||
$encryptedValue = EncryptService::encrypt($value); | ||
|
||
return self::orWhere($column, $operator, $encryptedValue); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
<?php | ||
|
||
namespace Joelwmale\LaravelEncryption\Builders; | ||
|
||
use Joelwmale\LaravelEncryption\Services\EncryptService; | ||
|
||
trait WhereInEncrypted | ||
{ | ||
public function whereInEncrypted($column, $values = []) | ||
{ | ||
$encryptedValues = []; | ||
|
||
foreach ($values as $value) { | ||
$encryptedValues[] = EncryptService::encrypt($value); | ||
} | ||
|
||
return self::whereIn($column, $encryptedValues); | ||
} | ||
|
||
public function orWhereInEncrypted($column, $values = []) | ||
{ | ||
$encryptedValues = []; | ||
|
||
foreach ($values as $value) { | ||
$encryptedValues[] = EncryptService::encrypt($value); | ||
} | ||
|
||
return self::orWhereIn($column, $encryptedValues); | ||
} | ||
|
||
public function whereNotInEncrypted($column, $values = []) | ||
{ | ||
$encryptedValues = []; | ||
|
||
foreach ($values as $value) { | ||
$encryptedValues[] = EncryptService::encrypt($value); | ||
} | ||
|
||
return self::whereNotIn($column, $encryptedValues); | ||
} | ||
|
||
public function orWhereNotInEncrypted($column, $values = []) | ||
{ | ||
$encryptedValues = []; | ||
|
||
foreach ($values as $value) { | ||
$encryptedValues[] = EncryptService::encrypt($value); | ||
} | ||
|
||
return self::orWhereNotIn($column, $encryptedValues); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<?php | ||
|
||
namespace Joelwmale\LaravelEncryption\Casts; | ||
|
||
class Date | ||
{ | ||
public static function handle($value) | ||
{ | ||
if (class_exists('Carbon\Carbon')) { | ||
return \Carbon\Carbon::parse($value); | ||
} | ||
|
||
return new \DateTime($value); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<?php | ||
|
||
namespace Joelwmale\LaravelEncryption\Casts; | ||
|
||
class DateTime | ||
{ | ||
public static function handle($value) | ||
{ | ||
if (class_exists('Carbon\Carbon')) { | ||
return \Carbon\Carbon::parse($value); | ||
} | ||
|
||
return new \DateTime($value); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
<?php | ||
|
||
namespace Joelwmale\LaravelEncryption\Services; | ||
|
||
class EncryptService | ||
{ | ||
public static function getKey() | ||
{ | ||
return config('laravel_encryption.key', config('app.key')); | ||
} | ||
|
||
public static function getCipher() | ||
{ | ||
$cipher = strtolower(config('laravel_encryption.cipher', 'AES-256-CBC')); | ||
|
||
if (! in_array($cipher, openssl_get_cipher_methods())) { | ||
throw new \Exception('The cipher method "'.$cipher.'" is not supported.'); | ||
} | ||
|
||
return $cipher; | ||
} | ||
|
||
public static function encrypt($string): string | ||
{ | ||
$iv = substr(md5(self::getKey()), 0, 16); | ||
|
||
$encrypted = openssl_encrypt($string, self::getCipher(), self::getKey(), 0, $iv); | ||
|
||
return base64_encode($encrypted); | ||
} | ||
|
||
public static function decrypt($encryptedString): string | ||
{ | ||
$iv = substr(md5(self::getKey()), 0, 16); | ||
|
||
$decrypted = openssl_decrypt(base64_decode($encryptedString), self::getCipher(), self::getKey(), 0, $iv); | ||
|
||
return $decrypted; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.