Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added .DS_Store
Binary file not shown.
38 changes: 22 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
# Cloak

![Cloak PHP Example](cloak-php.png)

A simple, extensible PHP package for redacting sensitive data from strings and revealing them later.

```php
use DynamikDev\Cloak\Cloak;

$cloak = Cloak::make();

$cloaked = $cloak->cloak('Email me at john@example.com');
$cloaked = cloak('Email me at john@example.com');
// "Email me at {{EMAIL_x7k2m9_1}}"

$original = $cloak->uncloak($cloaked);
$original = uncloak($cloaked);
// "Email me at john@example.com"
```

Expand All @@ -27,18 +25,14 @@ composer require dynamik-dev/cloak-php

## Quick Start

### Basic Usage
### Using Helper Functions

```php
use DynamikDev\Cloak\Cloak;

$cloak = Cloak::make();

$text = 'Contact: john@example.com, Phone: 555-123-4567';
$cloaked = $cloak->cloak($text);
$cloaked = cloak($text);
// "Contact: {{EMAIL_x7k2m9_1}}, Phone: {{PHONE_x7k2m9_1}}"

$original = $cloak->uncloak($cloaked);
$original = uncloak($cloaked);
// "Contact: john@example.com, Phone: 555-123-4567"
```

Expand All @@ -48,16 +42,29 @@ $original = $cloak->uncloak($cloaked);
use DynamikDev\Cloak\Detector;

// Only detect emails
$cloaked = $cloak->cloak($text, [Detector::email()]);
$cloaked = cloak($text, [Detector::email()]);

// Multiple detectors
$cloaked = $cloak->cloak($text, [
$cloaked = cloak($text, [
Detector::email(),
Detector::phone('US'),
Detector::ssn(),
]);
```

### Using the Cloak Class

For more control, use the `Cloak` class directly:

```php
use DynamikDev\Cloak\Cloak;

$cloak = Cloak::make();

$cloaked = $cloak->cloak($text);
$original = $cloak->uncloak($cloaked);
```

### Configuring with Builder Methods

```php
Expand All @@ -67,7 +74,6 @@ use DynamikDev\Cloak\Encryptors\OpenSslEncryptor;

$cloak = Cloak::make()
->withDetectors([Detector::email()])
->withTtl(7200)
->encrypt(OpenSslEncryptor::generateKey());

$cloaked = $cloak->cloak('Sensitive: john@example.com');
Expand Down
Binary file added cloak-php.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 4 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@
"autoload": {
"psr-4": {
"DynamikDev\\Cloak\\": "src/"
}
},
"files": [
"src/helpers.php"
]
},
"autoload-dev": {
"psr-4": {
Expand Down
35 changes: 35 additions & 0 deletions src/helpers.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

declare(strict_types=1);

use DynamikDev\Cloak\Cloak;
use DynamikDev\Cloak\Contracts\StoreInterface;

if (! function_exists('cloak')) {
/**
* Cloak sensitive data in text using the provided detectors.
*
* @param string $text The text to cloak
* @param array<int, \DynamikDev\Cloak\Contracts\DetectorInterface>|null $detectors Optional detectors to use
* @param StoreInterface|null $store Optional store instance
* @return string The cloaked text with placeholders
*/
function cloak(string $text, ?array $detectors = null, ?StoreInterface $store = null): string
{
return Cloak::make($store)->cloak($text, $detectors);
}
}

if (! function_exists('uncloak')) {
/**
* Uncloak text by replacing placeholders with original values.
*
* @param string $text The text containing placeholders
* @param StoreInterface|null $store Optional store instance
* @return string The uncloaked text with original values
*/
function uncloak(string $text, ?StoreInterface $store = null): string
{
return Cloak::make($store)->uncloak($text);
}
}
36 changes: 36 additions & 0 deletions tests/HelpersTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

declare(strict_types=1);

use DynamikDev\Cloak\Detector;

it('cloak helper function works', function () {
$text = 'Email: test@example.com';
$result = cloak($text);

expect($result)->toMatch('/Email: \{\{EMAIL_[a-zA-Z0-9]{6}_1\}\}/');
});

it('uncloak helper function works', function () {
$text = 'Email: test@example.com';
$cloaked = cloak($text);
$uncloaked = uncloak($cloaked);

expect($uncloaked)->toBe($text);
});

it('cloak helper accepts custom detectors', function () {
$text = 'Email: test@example.com SSN: 123-45-6789';
$result = cloak($text, [Detector::email()]);

expect($result)->toMatch('/\{\{EMAIL_[a-zA-Z0-9]{6}_1\}\}/');
expect($result)->toContain('123-45-6789');
});

it('helpers use default ArrayStore', function () {
$text = 'test@example.com';
$cloaked = cloak($text);

expect($cloaked)->toMatch('/\{\{EMAIL_[a-zA-Z0-9]{6}_1\}\}/');
expect(uncloak($cloaked))->toBe($text);
});