diff --git a/.github/workflows/php.yml b/.github/workflows/php.yml index d1fe3d6..44056bb 100644 --- a/.github/workflows/php.yml +++ b/.github/workflows/php.yml @@ -2,7 +2,7 @@ name: PHP Composer on: push: - branches: [ main ] + branches: [ main, development/* ] pull_request: branches: [ main ] diff --git a/CODEOWNERS b/CODEOWNERS new file mode 100644 index 0000000..5e6caf6 --- /dev/null +++ b/CODEOWNERS @@ -0,0 +1 @@ +*.php @MayMeow \ No newline at end of file diff --git a/composer.json b/composer.json index 488e519..0cf9928 100644 --- a/composer.json +++ b/composer.json @@ -21,9 +21,11 @@ "php": "7.4.*" }, "require-dev": { - "phpunit/phpunit": "^9.5" + "phpunit/phpunit": "^9.5", + "squizlabs/php_codesniffer": "^3.6" }, "scripts": { - "test": "phpunit tests" + "test": "phpunit tests", + "codesniffer": "phpcs --standard=PSR2 src" } } diff --git a/composer.lock b/composer.lock index af5f071..9870fda 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "b18fde7188b93b5dbedad6a02e292362", + "content-hash": "0aabbf8aff99cb016d4b4ef16ebf55e8", "packages": [], "packages-dev": [ { @@ -1912,6 +1912,62 @@ ], "time": "2020-09-28T06:39:44+00:00" }, + { + "name": "squizlabs/php_codesniffer", + "version": "3.6.0", + "source": { + "type": "git", + "url": "https://github.com/squizlabs/PHP_CodeSniffer.git", + "reference": "ffced0d2c8fa8e6cdc4d695a743271fab6c38625" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/squizlabs/PHP_CodeSniffer/zipball/ffced0d2c8fa8e6cdc4d695a743271fab6c38625", + "reference": "ffced0d2c8fa8e6cdc4d695a743271fab6c38625", + "shasum": "" + }, + "require": { + "ext-simplexml": "*", + "ext-tokenizer": "*", + "ext-xmlwriter": "*", + "php": ">=5.4.0" + }, + "require-dev": { + "phpunit/phpunit": "^4.0 || ^5.0 || ^6.0 || ^7.0" + }, + "bin": [ + "bin/phpcs", + "bin/phpcbf" + ], + "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": "lead" + } + ], + "description": "PHP_CodeSniffer tokenizes PHP, JavaScript and CSS files and detects violations of a defined set of coding standards.", + "homepage": "https://github.com/squizlabs/PHP_CodeSniffer", + "keywords": [ + "phpcs", + "standards" + ], + "support": { + "issues": "https://github.com/squizlabs/PHP_CodeSniffer/issues", + "source": "https://github.com/squizlabs/PHP_CodeSniffer", + "wiki": "https://github.com/squizlabs/PHP_CodeSniffer/wiki" + }, + "time": "2021-04-09T00:54:41+00:00" + }, { "name": "symfony/polyfill-ctype", "version": "v1.23.0", diff --git a/src/AESCryptoServiceProvider.php b/src/AESCryptoServiceProvider.php index a032930..2f2a4cf 100644 --- a/src/AESCryptoServiceProvider.php +++ b/src/AESCryptoServiceProvider.php @@ -84,7 +84,15 @@ public function generateIV() */ public function encrypt(string $plainText): string { - $encryptedBytes = openssl_encrypt($plainText, $this->cipher, $this->key, OPENSSL_RAW_DATA, $this->iv, $this->tag, $this->aad); + $encryptedBytes = openssl_encrypt( + $plainText, + $this->cipher, + $this->key, + OPENSSL_RAW_DATA, + $this->iv, + $this->tag, + $this->aad + ); return base64_encode($this->iv . $this->tag . $encryptedBytes); } @@ -103,6 +111,14 @@ public function decrypt(string $encryptedData): string $this->tag = substr($c, $iv_len, static::DEFAULT_GCM_TAG_LENGTH); $encryptedBytes = substr($c, $iv_len + static::DEFAULT_GCM_TAG_LENGTH); - return openssl_decrypt($encryptedBytes, $this->cipher, $this->key, OPENSSL_RAW_DATA, $this->iv, $this->tag, $this->aad); + return openssl_decrypt( + $encryptedBytes, + $this->cipher, + $this->key, + OPENSSL_RAW_DATA, + $this->iv, + $this->tag, + $this->aad + ); } -} \ No newline at end of file +} diff --git a/src/CryptoKey.php b/src/CryptoKey.php index 95ab042..bb66bac 100644 --- a/src/CryptoKey.php +++ b/src/CryptoKey.php @@ -5,7 +5,7 @@ class CryptoKey { - public function HelloWorld() : string + public function helloWorld() : string { return "Hello World"; } @@ -17,8 +17,12 @@ public function HelloWorld() : string * @param int $length * @return string */ - public function getCryptographicKey(string $password, ?string $salt = null, int $iterations = 1024, int $length = 48) : string - { + public function getCryptographicKey( + string $password, + ?string $salt = null, + int $iterations = 1024, + int $length = 48 + ) : string { return hash_pbkdf2("sha256", $password, $salt, $iterations, $length); } -} \ No newline at end of file +} diff --git a/src/RSACryptoServiceProvider.php b/src/RSACryptoServiceProvider.php new file mode 100644 index 0000000..4cb03db --- /dev/null +++ b/src/RSACryptoServiceProvider.php @@ -0,0 +1,109 @@ +parameters = $parameters; + } + + /** + * encrypt file with public key + */ + public function encrypt($plainText) : string + { + $encrypted = ''; + + openssl_public_encrypt($plainText, $encrypted, $this->parameters->getPublicKey()); + + return base64_encode($encrypted); + } + + /** + * decrypt with private key + */ + public function decrypt($encryptedText) : string + { + $plainText = ''; + $privKey = $this->parameters->getPrivateKey(); + + openssl_private_decrypt(base64_decode($encryptedText), $plainText, $privKey); + + return $plainText; + } + + public function private_encrypt($plainText) : string + { + $encrypted = ''; + $privKey = $this->parameters->getPrivateKey(); + + openssl_private_encrypt($plainText, $encrypted, $privKey); + + return base64_encode($encrypted); + } + + public function public_decrypt($encryptedText) : string + { + $plainText = ''; + openssl_public_decrypt(base64_decode($encryptedText), $plainText, $this->parameters->getPublicKey()); + + return $plainText; + } + + protected function seal(string $plain_text) : string + { + //openssl_open($plain_text, $sealed_data, $ekeys, [$this->parameters->getPrivateKey()]) + } + + protected function open() + { + + } + + /** + * @param $data + * @return string + */ + public function sign($data) : string + { + $privKey = $this->_getPrivateKey(); + + $result = openssl_sign($data, $signature, $privKey, OPENSSL_ALGO_SHA512); + + return base64_encode($signature); + } + + /** + * @param $data + * @param $signature + * @return bool + */ + public function verify($data, $signature) : bool + { + $verification = openssl_verify($data, base64_decode($signature), $this->parameters->getPublicKey(), OPENSSL_ALGO_SHA512); + + return (bool)$verification; + } + + /** + * @return string + */ + public function getFingerPrint() : string + { + $fingerprint = join(':', str_split(md5(base64_decode($this->parameters->getPublicKey())), 2)); + + return $fingerprint; + } + + protected function _getPrivateKey() + { + return $this->parameters->getPrivateKey(); + } +} \ No newline at end of file diff --git a/src/RSAParameters.php b/src/RSAParameters.php new file mode 100644 index 0000000..9eca154 --- /dev/null +++ b/src/RSAParameters.php @@ -0,0 +1,103 @@ + 'sha512', + 'private_key_bits' => 4096, + 'private_key_type' => OPENSSL_KEYTYPE_RSA, + ]; + + public function __construct() + { + } + + public function generateKeys(?string $passphrase = null, ?array $configArgs = null) : void + { + $keys = openssl_pkey_new($this->config); + + if ($passphrase != null) { + $this->passphrase = $passphrase; + } + + openssl_pkey_export($keys, $private, $passphrase, $configArgs); + $this->privateKey = $private; + + $pub = openssl_pkey_get_details($keys); + $this->publicKey = $pub['key']; + } + + /** + * @return string + */ + public function getPrivateKey() + { + if ($this->passphrase != null && $this->privateKey != null) { + return openssl_pkey_get_private($this->privateKey, $this->passphrase); + } + + return $this->publicKey; + } + + /** + * @param string $privateKey + */ + public function setPrivateKey(string $privateKey): void + { + $this->privateKey = $privateKey; + } + + /** + * @return string + */ + public function getPublicKey(): string + { + return $this->publicKey; + } + + /** + * @param string $publicKey + */ + public function setPublicKey(string $publicKey): void + { + $this->publicKey = $publicKey; + } + + /** + * @return string + */ + public function getPassphrase(): string + { + return $this->passphrase; + } + + /** + * @param string $passphrase + */ + public function setPassphrase(string $passphrase): void + { + $this->passphrase = $passphrase; + } + + /** + * @return array + */ + public function getConfig(): array + { + return $this->config; + } + + /** + * @param array $config + */ + public function setConfig(array $config): void + { + $this->config = $config; + } +} diff --git a/tests/PBKDF2Test.php b/tests/PBKDF2Test.php index e5c7eca..86be572 100644 --- a/tests/PBKDF2Test.php +++ b/tests/PBKDF2Test.php @@ -13,7 +13,7 @@ public function TestHelloWorld() :void { $p = new CryptoKey(); - $this->assertEquals("Hello World", $p->HelloWorld()); + $this->assertEquals("Hello World", $p->helloWorld()); } /** @test */ diff --git a/tests/RSACryptoServiceProviderTest.php b/tests/RSACryptoServiceProviderTest.php new file mode 100644 index 0000000..48d2f0f --- /dev/null +++ b/tests/RSACryptoServiceProviderTest.php @@ -0,0 +1,24 @@ +generateKeys("passphrase"); + + $rsa = new RSACryptoServiceProvider(); + $rsa->setParameters($parameters); + $encryptedTest = $rsa->encrypt($plainText); + + $this->assertEquals($plainText, $rsa->decrypt($encryptedTest)); + } +} \ No newline at end of file diff --git a/tests/RSAParametersTest.php b/tests/RSAParametersTest.php new file mode 100644 index 0000000..4bc014c --- /dev/null +++ b/tests/RSAParametersTest.php @@ -0,0 +1,18 @@ +generateKeys(); + + $this->assertInstanceOf(RSAParameters::class, $parameters); + } +} \ No newline at end of file