Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/smoren/encryption-tools-php
Tools for encryption/decryption and signing/verifying (wraps openssl lib). Symmetric and asymmetric (RSA-based) encryption.
https://github.com/smoren/encryption-tools-php
cryptography encryption rsa
Last synced: about 2 months ago
JSON representation
Tools for encryption/decryption and signing/verifying (wraps openssl lib). Symmetric and asymmetric (RSA-based) encryption.
- Host: GitHub
- URL: https://github.com/smoren/encryption-tools-php
- Owner: Smoren
- License: mit
- Created: 2021-08-10T20:06:25.000Z (over 3 years ago)
- Default Branch: master
- Last Pushed: 2023-10-25T09:05:58.000Z (about 1 year ago)
- Last Synced: 2024-04-25T15:20:48.629Z (9 months ago)
- Topics: cryptography, encryption, rsa
- Language: PHP
- Homepage:
- Size: 45.9 KB
- Stars: 12
- Watchers: 6
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# encryption-tools
![Packagist PHP Version Support](https://img.shields.io/packagist/php-v/smoren/encryption-tools)
[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/Smoren/encryption-tools-php/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/Smoren/encryption-tools-php/?branch=master)
[![Coverage Status](https://coveralls.io/repos/github/Smoren/encryption-tools-php/badge.svg?branch=master)](https://coveralls.io/github/Smoren/encryption-tools-php?branch=master)
![Build and test](https://github.com/Smoren/encryption-tools-php/actions/workflows/test_master.yml/badge.svg)
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)Tools for encryption/decryption and signing/verifying (wraps openssl lib).
* Symmetric
* Asymmetric (RSA-based)### Install to your project
```shell script
composer require smoren/encryption-tools
```### Unit testing
```shell script
composer install
composer test-init
composer test
```### Usage
#### Symmetric encryption/decryption
```php
use Smoren\EncryptionTools\Helpers\SymmetricEncryptionHelper;$data = ["some", "data" => "to", "encrypt"];
$secretKey = uniqid();$dataEncrypted = SymmetricEncryptionHelper::encrypt($data, $secretKey);
$dataDecrypted = SymmetricEncryptionHelper::decrypt($dataEncrypted, $secretKey);
print_r($dataDecrypted);$dataEncrypted = SymmetricEncryptionHelper::encrypt($data, $secretKey, 'camellia-256-ofb');
$dataDecrypted = SymmetricEncryptionHelper::decrypt($dataEncrypted, $secretKey, 'camellia-256-ofb');
print_r($dataDecrypted);
```#### Asymmetric encryption/decryption (RSA-based)
```php
use Smoren\EncryptionTools\Helpers\AsymmetricEncryptionHelper;$data = ["some", "data" => "to", "encrypt"];
[$privateKey, $publicKey] = AsymmetricEncryptionHelper::generateKeyPair();$dataEncrypted = AsymmetricEncryptionHelper::encryptByPrivateKey($data, $privateKey);
$dataDecrypted = AsymmetricEncryptionHelper::decryptByPublicKey($dataEncrypted, $publicKey);
print_r($dataDecrypted);$dataEncrypted = AsymmetricEncryptionHelper::encryptByPublicKey($data, $publicKey);
$dataDecrypted = AsymmetricEncryptionHelper::decryptByPrivateKey($dataEncrypted, $privateKey);
print_r($dataDecrypted);
```#### Asymmetric signing/verifying (RSA-based)
```php
use Smoren\EncryptionTools\Helpers\AsymmetricEncryptionHelper;
use Smoren\EncryptionTools\Exceptions\AsymmetricEncryptionException;$data = ["some", "data" => "to", "encrypt"];
[$privateKey, $publicKey] = AsymmetricEncryptionHelper::generateKeyPair();$signature = AsymmetricEncryptionHelper::sign($data, $privateKey);
try {
AsymmetricEncryptionHelper::verify($data, $signature, $publicKey);
} catch(AsymmetricEncryptionException $e) {
// ... handling exception if cannot verify signature
}
```#### Asymmetric encryption/decryption (RSA-based) for large data
```php
use Smoren\EncryptionTools\Helpers\AsymmetricLargeDataEncryptionHelper;$data = file_get_contents('file_with_large_data.txt');
[$privateKey, $publicKey] = AsymmetricLargeDataEncryptionHelper::generateKeyPair();$dataEncrypted = AsymmetricLargeDataEncryptionHelper::encryptByPrivateKey($data, $privateKey);
$dataDecrypted = AsymmetricLargeDataEncryptionHelper::decryptByPublicKey($dataEncrypted, $publicKey);
print_r($dataDecrypted);$dataEncrypted = AsymmetricLargeDataEncryptionHelper::encryptByPublicKey($data, $publicKey);
$dataDecrypted = AsymmetricLargeDataEncryptionHelper::decryptByPrivateKey($dataEncrypted, $privateKey);
print_r($dataDecrypted);
```