Skip to content

Commit c052516

Browse files
author
howya
authored
Merge pull request #5 from howya/develop
Develop
2 parents 7d57014 + 695dac0 commit c052516

File tree

1 file changed

+77
-0
lines changed

1 file changed

+77
-0
lines changed

README.md

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,79 @@
11
# laravel_model_encryption
22
A no fuss Laravel model trait to add encrypted model attributes with the option of blind indexes and hashed attributes.
3+
4+
Model attributes that are marked as encrypted will be encrypted via Laravel's default encryption. Simply set the model attribute with a value of string, integer, float, boolean or date (as a string) and the value will be persisted as the encrypted value. Accessing the attribute will decrypt the attribute and cast it to it's defined type (a Carbon instance if date).
5+
6+
You may also specify a blind index (BI) attribute that is associated with encrypted attribute. Blind index attributes persist the hashed value of the associated plain text encrypted attribute column. BI attributes can be used for searching the database where search values are encrypted. A 'whereBI' local scope has been added that allows 'where' clauses to be specified using the encrypted attribute name and plain text value.
7+
8+
Finally, you may specify hashed attributes. These are simply hashed before persistence and cannot be converted to plain text on retrieval.
9+
10+
Note - adding a blind index will reduce the security of your encrypted columns as the associated BI is deterministic and will identify collisions in your encrypted data. This is by design, without it your encrypted data would not be searchable.
11+
12+
Note - you may change the hashing algorithm by setting the $hashAlg property on your model, this is set to 'sha256' by default. if you increase the security of the algorithm used then you may have to increase the column size for BI columns. I have found that length 64 works well for sha256.
13+
14+
Note - this trait does not create DB columns for you. You must create your own migrations. Please see Fixtures/Migrations/2018_04_18_134800_create_test_table.php for an idea of column definitions that fit with the attributes defined within Fixtures/Models/TestModel.php
15+
16+
# Requirements
17+
This package requires Laravel 5.6. Earlier versions of Laravel have a different model implementation that is not compatible with this package.
18+
19+
# Installation
20+
```
21+
composer require *****
22+
```
23+
24+
# How to use
25+
##Add the trait to your model
26+
```
27+
use Rbennett\HasEncryptedAttributes;
28+
use Illuminate\Database\Eloquent\Model;
29+
30+
class TestModel extends Model
31+
{
32+
use HasEncryptedAttributes;
33+
```
34+
35+
## Add Encrypted database columns
36+
For any DB columns that should be encrypted / decrypted add them to the $encrypted property of the model.
37+
This should be set to an array of form:
38+
39+
['actual_column_name' => ['type' => 'string|integer|float|boolean|date', {'hasBlindIndex' => 'blind_index_column_name}]]
40+
41+
Note - hasBlindIndex is optional
42+
Note - if adding a date then specifiy 'dateformat'
43+
44+
See example below:
45+
46+
```
47+
protected $encrypted = [
48+
'encrypt_string' =>
49+
['type' => 'string', 'hasBlindIndex' => 'encrypt_string_bi'],
50+
'encrypt_integer' =>
51+
['type' => 'integer', 'hasBlindIndex' => 'encrypt_integer_bi'],
52+
'encrypt_boolean' =>
53+
['type' => 'boolean', 'hasBlindIndex' => 'encrypt_boolean_bi'],
54+
'encrypt_another_boolean' =>
55+
['type' => 'boolean'],
56+
'encrypt_float' =>
57+
['type' => 'float', 'hasBlindIndex' => 'encrypt_float_bi'],
58+
'encrypt_date' =>
59+
['type' => 'date', 'hasBlindIndex' => 'encrypt_date_bi', 'dateFormat' => 'Y-m-d H:i:s']
60+
];
61+
```
62+
63+
## Add Hashed database columns
64+
65+
For any DB columns that are to be hashed, simply add them to the $hashed property of the model:
66+
67+
```
68+
protected $hashed = ['column_name1', 'column_name2];
69+
```
70+
71+
## Local scope helper
72+
A local scope has been added that allows you to query BI columns (whereBI). When using whereBI, pass in an array where the key is the encrypted column name (it will resolve the associated BI column for you) and the value you want to search for. The value should be passed in as plain text in one of the following types:
73+
74+
string, integer, float, boolean or date (as a string)
75+
76+
When querying for dates ensure that the date format is identical to the dateformat set within the $encrypted property.
77+
78+
79+

0 commit comments

Comments
 (0)