Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
nabeghe committed Oct 19, 2024
0 parents commit ecbb439
Show file tree
Hide file tree
Showing 8 changed files with 2,204 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.idea
.phpunit.result.cache
/vendor/
*.secret.php
21 changes: 21 additions & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2023 Hadi Akbarzadeh

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
110 changes: 110 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
# Simple Cipher for PHP

> A simple cipher using OpenSSL with a few additional tweaks.
Implementing a cipher to encrypt and decrypt your data can be a bit tricky, but with OpenSSL, it's a breeze.
However, this library combines several techniques to give you a simple yet effective way to handle encryption and
decryption.
You don’t even need to set a secret or password for your cipher system!
If you don’t specify one,
a default value is used that’s unique and remains constant throughout the program, except in special cases.
However, it's not recommended

The best part? You'll be working with a SimpleCipher class here,
where its methods are accessible both statically and through object instances.
Data is returned in the same type it was encrypted with, ensuring consistency.
but objects converted into arrays, and you also have the option to set a default value in case there’s an issue with
decryption.

<hr>

## 🫡 Usage

### 🚀 Installation

You can install the package via composer:

```bash
composer require nabeghe/simple-cipher
```

<hr>

### Configuration

The configuration can be an array, string, number, or null.
A string or number means setting the secret (password), null means using default values, and an array allows for setting each option separately.

#### Array Syntax:

| Optiona name | Description |
|---------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| algo | The cipher method.<br>Default: aes-256-gcm |
| secret | The cipher secret (password).<br>Default: A random value is generated at the start & stored in a file within the library's directory. However, the priority is first given to a constant named `SIMPLE_CIPHER_SECRET`, , and then to an environment key with the same name in `$_ENV`. |
| default | A default value is used in case there’s an issue in the decryption process, which is null if not set. |

### Static Mode

#### Syntax:

```php
SimpleCipher::encrypt(mixed $data, array|string|int|null $config): string;

SimpleCipher::decrypt(string $data, array|string|int|null $config): mixed;
```

#### Example:

```php
use Nabeghe\SimpleCipher\SimpleCipher;

$data = 'nabeghe/simple-cipher';

$encrypted = SimpleCipher::encrypt($data, 'YOUR_SECRET');
echo "Encrypted Data:\n";
echo "$encrypted\n\n";

$decrypted = SimpleCipher::decrypt($encrypted, 'YOUR_SECRET');
echo "Decrypted Data:\n";
var_dump($decrypted);
```

<hr>

### Instance Mode

#### Syntax:

```php
__construct(array|string|int|null $config = null)

$cipher->encrypt(mixed $data): string;

$cipher->decrypt(string $data, mixed $default = null): mixed;
```

#### Example:

```php
use Nabeghe\SimpleCipher\SimpleCipher;

$data = 'nabeghe/simple-cipher';

$cipher = new SimpleCipher('YOUR_SECRET');

$encrypted = $cipher->encrypt($data);
echo "Encrypted Data:\n";
echo "$encrypted\n\n";

$decrypted = $cipher->decrypt($encrypted);
echo "Decrypted Data:\n";
var_dump($decrypted);
```

<hr>

## 📖 License

Copyright (c) 2024 Hadi Akbarzadeh

Licensed under the MIT license, see [LICENSE.md](LICENSE.md) for details.
33 changes: 33 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{
"name": "nabeghe/simple-cipher",
"description": "A simple cipher using OpenSSL with a few additional tweaks.",
"keywords": [
"cipher", "encrypt", "decryot", "encryption", "decryption", "openssl", "password", "secret", "security",
"helper", "support", "library"
],
"type": "library",
"version": "1.0.0",
"homepage": "https://github.com/nabeghe/simple-cipher-php",
"license": "MIT",
"autoload": {
"psr-4": {
"Nabeghe\\SimpleCipher\\": "src/"
}
},
"authors": [
{
"name": "Hadi Akbarzadeh",
"email": "[email protected]",
"homepage": "https://elatel.ir",
"role": "Developer"
}
],
"require": {
"php": ">=7.4",
"ext-openssl": "*",
"ext-json": "*"
},
"require-dev": {
"phpunit/phpunit": "9.6"
}
}
Loading

0 comments on commit ecbb439

Please sign in to comment.