PHP Classes

File: README.md

Recommend this page to a friend!
  Classes of Smoren Freelight   PHP Encryption Tools   README.md   Download  
File: README.md
Role: Documentation
Content type: text/markdown
Description: Documentation
Class: PHP Encryption Tools
Encrypt, decrypt, sign, verify data with OpenSSL
Author: By
Last change:
Date: 1 year ago
Size: 3,541 bytes
 

Contents

Class file image Download

encryption-tools

Packagist PHP Version Support Scrutinizer Code Quality Coverage Status Build and test License: MIT

Tools for encryption/decryption and signing/verifying (wraps openssl lib).

  • Symmetric
  • Asymmetric (RSA-based)

Install to your project

composer require smoren/encryption-tools

Unit testing

composer install
composer test-init
composer test

Usage

Symmetric encryption/decryption

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)

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)

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 lage data

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);