initial commit
All checks were successful
continuous-integration/drone/push Build is passing
continuous-integration/drone Build is passing

This commit is contained in:
2024-02-16 15:44:24 -06:00
commit 19849f3752
8 changed files with 350 additions and 0 deletions

14
.drone.yml Normal file
View File

@@ -0,0 +1,14 @@
kind: pipeline
type: docker
name: default
steps:
- name: install
image: composer
commands:
- composer install
- name: Run Rector
image: php:7.4
commands:
- vendor/bin/rector --dry-run

5
.gitignore vendored Normal file
View File

@@ -0,0 +1,5 @@
# ---> Composer
composer.phar
/vendor/
.DS_Store
.env

9
LICENSE Normal file
View File

@@ -0,0 +1,9 @@
MIT License
Copyright (c) 2024 hpz937
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.

2
README.md Normal file
View File

@@ -0,0 +1,2 @@
# encryption

23
composer.json Normal file
View File

@@ -0,0 +1,23 @@
{
"name": "hpz937/encryption",
"type": "library",
"license": "MIT",
"autoload": {
"psr-4": {
"Hpz937\\Encryption\\": "src/"
}
},
"authors": [
{
"name": "Brad Cimbura",
"email": "hpz937@gmail.com"
}
],
"require": {
"php": ">=7.4"
},
"require-dev": {
"rector/rector": "^1.0",
"squizlabs/php_codesniffer": "^3.9"
}
}

217
composer.lock generated Normal file
View File

@@ -0,0 +1,217 @@
{
"_readme": [
"This file locks the dependencies of your project to a known state",
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically"
],
"content-hash": "9ef6a3d22757c7ee16fa9a8ebb911af9",
"packages": [],
"packages-dev": [
{
"name": "phpstan/phpstan",
"version": "1.10.58",
"source": {
"type": "git",
"url": "https://github.com/phpstan/phpstan.git",
"reference": "a23518379ec4defd9e47cbf81019526861623ec2"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/phpstan/phpstan/zipball/a23518379ec4defd9e47cbf81019526861623ec2",
"reference": "a23518379ec4defd9e47cbf81019526861623ec2",
"shasum": ""
},
"require": {
"php": "^7.2|^8.0"
},
"conflict": {
"phpstan/phpstan-shim": "*"
},
"bin": [
"phpstan",
"phpstan.phar"
],
"type": "library",
"autoload": {
"files": [
"bootstrap.php"
]
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"description": "PHPStan - PHP Static Analysis Tool",
"keywords": [
"dev",
"static analysis"
],
"support": {
"docs": "https://phpstan.org/user-guide/getting-started",
"forum": "https://github.com/phpstan/phpstan/discussions",
"issues": "https://github.com/phpstan/phpstan/issues",
"security": "https://github.com/phpstan/phpstan/security/policy",
"source": "https://github.com/phpstan/phpstan-src"
},
"funding": [
{
"url": "https://github.com/ondrejmirtes",
"type": "github"
},
{
"url": "https://github.com/phpstan",
"type": "github"
},
{
"url": "https://tidelift.com/funding/github/packagist/phpstan/phpstan",
"type": "tidelift"
}
],
"time": "2024-02-12T20:02:57+00:00"
},
{
"name": "rector/rector",
"version": "1.0.1",
"source": {
"type": "git",
"url": "https://github.com/rectorphp/rector.git",
"reference": "258b775511e62a7188f8ce114d44acaf244d9a7d"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/rectorphp/rector/zipball/258b775511e62a7188f8ce114d44acaf244d9a7d",
"reference": "258b775511e62a7188f8ce114d44acaf244d9a7d",
"shasum": ""
},
"require": {
"php": "^7.2|^8.0",
"phpstan/phpstan": "^1.10.57"
},
"conflict": {
"rector/rector-doctrine": "*",
"rector/rector-downgrade-php": "*",
"rector/rector-phpunit": "*",
"rector/rector-symfony": "*"
},
"bin": [
"bin/rector"
],
"type": "library",
"autoload": {
"files": [
"bootstrap.php"
]
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"description": "Instant Upgrade and Automated Refactoring of any PHP code",
"keywords": [
"automation",
"dev",
"migration",
"refactoring"
],
"support": {
"issues": "https://github.com/rectorphp/rector/issues",
"source": "https://github.com/rectorphp/rector/tree/1.0.1"
},
"funding": [
{
"url": "https://github.com/tomasvotruba",
"type": "github"
}
],
"time": "2024-02-16T07:53:23+00:00"
},
{
"name": "squizlabs/php_codesniffer",
"version": "3.9.0",
"source": {
"type": "git",
"url": "https://github.com/PHPCSStandards/PHP_CodeSniffer.git",
"reference": "d63cee4890a8afaf86a22e51ad4d97c91dd4579b"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/PHPCSStandards/PHP_CodeSniffer/zipball/d63cee4890a8afaf86a22e51ad4d97c91dd4579b",
"reference": "d63cee4890a8afaf86a22e51ad4d97c91dd4579b",
"shasum": ""
},
"require": {
"ext-simplexml": "*",
"ext-tokenizer": "*",
"ext-xmlwriter": "*",
"php": ">=5.4.0"
},
"require-dev": {
"phpunit/phpunit": "^4.0 || ^5.0 || ^6.0 || ^7.0 || ^8.0 || ^9.3.4"
},
"bin": [
"bin/phpcbf",
"bin/phpcs"
],
"type": "library",
"extra": {
"branch-alias": {
"dev-master": "3.x-dev"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"BSD-3-Clause"
],
"authors": [
{
"name": "Greg Sherwood",
"role": "Former lead"
},
{
"name": "Juliette Reinders Folmer",
"role": "Current lead"
},
{
"name": "Contributors",
"homepage": "https://github.com/PHPCSStandards/PHP_CodeSniffer/graphs/contributors"
}
],
"description": "PHP_CodeSniffer tokenizes PHP, JavaScript and CSS files and detects violations of a defined set of coding standards.",
"homepage": "https://github.com/PHPCSStandards/PHP_CodeSniffer",
"keywords": [
"phpcs",
"standards",
"static analysis"
],
"support": {
"issues": "https://github.com/PHPCSStandards/PHP_CodeSniffer/issues",
"security": "https://github.com/PHPCSStandards/PHP_CodeSniffer/security/policy",
"source": "https://github.com/PHPCSStandards/PHP_CodeSniffer",
"wiki": "https://github.com/PHPCSStandards/PHP_CodeSniffer/wiki"
},
"funding": [
{
"url": "https://github.com/PHPCSStandards",
"type": "github"
},
{
"url": "https://github.com/jrfnl",
"type": "github"
},
{
"url": "https://opencollective.com/php_codesniffer",
"type": "open_collective"
}
],
"time": "2024-02-16T15:06:51+00:00"
}
],
"aliases": [],
"minimum-stability": "stable",
"stability-flags": [],
"prefer-stable": false,
"prefer-lowest": false,
"platform": [],
"platform-dev": [],
"plugin-api-version": "2.6.0"
}

10
rector.php Normal file
View File

@@ -0,0 +1,10 @@
<?php
use Rector\Config\RectorConfig;
return RectorConfig::configure()
->withPaths([__DIR__ . '/src'])
->withPhpSets()
->withPreparedSets(false, true)
->withAttributesSets(true, true)
->withImportNames(false, false, true, true);

70
src/DataEncryptor.php Normal file
View File

@@ -0,0 +1,70 @@
<?php
namespace Hpz937\Encryption;
/**
* Class DataEncryptor provides methods to encrypt and decrypt data using OpenSSL with AES-256-CBC encryption.
*/
class DataEncryptor {
/**
* @var string The encryption method to use.
*/
private $encryptMethod = "AES-256-CBC";
/**
* @var string The secret key for encryption and decryption.
*/
private $secretKey;
/**
* Constructs the DataEncryptor object with a secret key.
*
* @param string $key The secret key used for encryption and decryption. It is recommended to be a strong, unique key.
*/
public function __construct(string $key)
{
$this->secretKey = hash('sha256', $key);
}
/**
* Encrypts the given data using AES-256-CBC encryption and returns the base64 encoded encrypted data with the IV prepended.
*
* @param string $data The plaintext data to encrypt.
* @return string The base64 encoded encrypted data with the IV prepended.
*/
public function encrypt($data)
{
// Generate a unique IV for each encryption
$iv = openssl_random_pseudo_bytes(openssl_cipher_iv_length($this->encryptMethod));
// Encrypt the data
$encryptedData = openssl_encrypt($data, $this->encryptMethod, $this->secretKey, 0, $iv);
// Prepend the IV to the encrypted data for storage
$encryptedDataWithIv = base64_encode($iv . $encryptedData);
return $encryptedDataWithIv;
}
/**
* Decrypts the given data, which includes the IV and the encrypted data, using AES-256-CBC decryption.
*
* @param string $dataWithIv The base64 encoded encrypted data with the IV prepended.
* @return string The decrypted plaintext data.
*/
public function decrypt($dataWithIv)
{
// Decode from base64
$dataWithIv = base64_decode($dataWithIv);
// Extract the IV and encrypted data
$ivLength = openssl_cipher_iv_length($this->encryptMethod);
$iv = substr($dataWithIv, 0, $ivLength);
$encryptedData = substr($dataWithIv, $ivLength);
// Decrypt the data
$decryptedData = openssl_decrypt($encryptedData, $this->encryptMethod, $this->secretKey, 0, $iv);
return $decryptedData;
}
}