Skip to content

Commit 882e8bb

Browse files
committed
develop/v0.1.0: Added search on encrypted features
1 parent aab10a7 commit 882e8bb

File tree

2 files changed

+45
-0
lines changed

2 files changed

+45
-0
lines changed

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ A Laravel package for secure, transparent encryption and decryption of sensitive
77
- Encrypted data is stored in a separate `encrypted_attributes` table
88
- Only non-table attributes can be encrypted (enforced at runtime)
99
- Automatic loading and saving of encrypted attributes using Eloquent events
10+
- **Search/filter on encrypted properties using SHA-256 hash**
1011
- No sensitive values are ever logged
1112
- Easy integration: just add the trait and define `$encryptedProperties` in your model
1213
- Compatible with Laravel 9+
@@ -21,6 +22,7 @@ A Laravel package for secure, transparent encryption and decryption of sensitive
2122
2. Define a public array property `$encryptedProperties` listing the attributes you want encrypted (these must NOT exist as columns in the model's table).
2223
3. When you load a model, encrypted attributes are automatically decrypted and available as normal properties.
2324
4. When you save a model, encrypted attributes are removed from the main table and securely stored in the `encrypted_attributes` table.
25+
5. **You can filter/search on encrypted properties using the provided query scope.**
2426

2527
**Example Model:**
2628
```php
@@ -54,6 +56,16 @@ echo $user->social_security_number; // '123-45-6789'
5456
```
5557
- If you try to add an attribute to `$encryptedProperties` that already exists as a column, an exception will be thrown.
5658

59+
### Filtering/Search on Encrypted Properties
60+
You can filter or search for models by encrypted property value using the built-in query scope:
61+
62+
```php
63+
// Find users with a specific social security number
64+
$users = User::whereEncrypted('social_security_number', '123-45-6789')->get();
65+
```
66+
67+
This uses the SHA-256 hash of the value and joins the `encrypted_attributes` table for efficient searching, without ever exposing the decrypted value in the query or logs.
68+
5769
## Installation Steps
5870
1. Require the package in your Laravel project:
5971
```sh

src/Traits/HasEncryptedAttributes.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
namespace Wazza\DbEncrypt\Traits;
44

55
use Wazza\DbEncrypt\Http\Controllers\DbEncryptController;
6+
use Wazza\DbEncrypt\Models\EncryptedAttributes;
7+
use Wazza\DbEncrypt\Helper\Encryptor;
68

79
/**
810
* Include this trait in your model to enable database encryption functionality.
@@ -105,4 +107,35 @@ public function saveEncryptedAttributes(): void
105107

106108
$this->_encryptedAttributesBuffer = [];
107109
}
110+
111+
/**
112+
* Scope a query to filter by an encrypted property.
113+
* Example: $users = User::whereEncrypted('social_security_number', '123-45-6789')->get();
114+
*
115+
* @param \Illuminate\Database\Eloquent\Builder $query
116+
* @param string $attribute
117+
* @param string $value
118+
* @return \Illuminate\Database\Eloquent\Builder
119+
*/
120+
public function scopeWhereEncrypted($query, string $attribute, string $value)
121+
{
122+
$hash = Encryptor::hash($value);
123+
124+
return $query->whereHas('encryptedAttributesRelation', function ($q) use ($attribute, $hash) {
125+
$q->where('attribute', $attribute)
126+
->where('hash_index', $hash);
127+
});
128+
}
129+
130+
/**
131+
* Define a relationship to the encrypted_attributes table.
132+
*/
133+
public function encryptedAttributesRelation()
134+
{
135+
return $this->hasMany(
136+
EncryptedAttributes::class,
137+
'object_id',
138+
$this->getKeyName()
139+
)->where('object_type', $this->getTable());
140+
}
108141
}

0 commit comments

Comments
 (0)