Skip to content

🔧 add phpstan #6

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Oct 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion .github/workflows/php.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,22 @@ jobs:
composer_script: 'test'


qa:
codesniffer:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Composer run action PHPC_CS
uses: MayMeowHQ/composer-run-action@v1
with:
composer_script: 'codesniffer'

stan:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Composer run action PHPStan
uses: MayMeowHQ/composer-run-action@v1
with:
composer_script: 'stan'


6 changes: 4 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,12 @@
},
"require-dev": {
"phpunit/phpunit": "^9.5",
"squizlabs/php_codesniffer": "^3.6"
"squizlabs/php_codesniffer": "^3.6",
"phpstan/phpstan": "^0.12.99"
},
"scripts": {
"test": "phpunit tests",
"codesniffer": "phpcs --standard=PSR2 src"
"codesniffer": "phpcs --standard=PSR2 src",
"stan": "phpstan analyse"
}
}
66 changes: 65 additions & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
parameters:
level: 7
checkMissingIterableValueType: false
treatPhpDocTypesAsCertain: false
paths:
- src
61 changes: 46 additions & 15 deletions src/AESCryptoServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,25 @@

namespace MayMeow\Cryptography;

use MayMeow\Cryptography\Exceptions\DecryptException;
use MayMeow\Cryptography\Exceptions\IvGenerateException;

class AESCryptoServiceProvider
{
const CIPHER_TYPE_GCM = 'aes-256-gcm';
const DEFAULT_GCM_TAG_LENGTH = 16;

protected $cipher;
protected string $cipher;

protected $iv;
protected string $iv;

protected $key;
protected string $key;

protected $aad = "127.0.0.1";
protected string $aad = "127.0.0.1";

protected $tag;
protected string $tag = '';

public function __construct($cipher = null)
public function __construct(string $cipher = null)
{
if ($cipher == null) {
$this->cipher = static::CIPHER_TYPE_GCM;
Expand All @@ -27,34 +30,42 @@ public function __construct($cipher = null)
}

/**
* @param $iv
* Set IV
*
* @param string $iv
* @return AESCryptoServiceProvider
*/
public function setIV($iv): AESCryptoServiceProvider
public function setIV(string $iv): AESCryptoServiceProvider
{
$this->iv = $iv;

return $this;
}

/**
* @param $key
* Set key needed for encryption
*
* @param string $key
* @return AESCryptoServiceProvider
*/
public function setKey($key): AESCryptoServiceProvider
public function setKey(string $key): AESCryptoServiceProvider
{
$this->key = $key;

return $this;
}

/**
* Generate key
*
* @return bool|string
*/
public function generateKey()
{
if (in_array($this->cipher, openssl_get_cipher_methods())) {
$this->key = openssl_random_pseudo_bytes(32);
if ($key = openssl_random_pseudo_bytes(32)) {
$this->key = $key;
}

return $this->key;
}
Expand All @@ -63,12 +74,18 @@ public function generateKey()
}

/**
* Generate IV
*
* @return bool|string
*/
public function generateIV()
{
if (in_array($this->cipher, openssl_get_cipher_methods())) {
$this->iv = openssl_random_pseudo_bytes(openssl_cipher_iv_length($this->cipher));
if ($ivLength = openssl_cipher_iv_length($this->cipher)) {
if ($iv = openssl_random_pseudo_bytes($ivLength)) {
$this->iv = $iv;
}
}

return $this->iv;
}
Expand Down Expand Up @@ -98,20 +115,28 @@ public function encrypt(string $plainText): string
}

/**
* Returns decrypted text
* Decrypt given text
*
* @param string $encryptedData
* @return string
* @throws DecryptException
* @throws IvGenerateException
*/
public function decrypt(string $encryptedData): string
{
$c = base64_decode($encryptedData);

$iv_len = openssl_cipher_iv_length($this->cipher);
if ($ivLength = openssl_cipher_iv_length($this->cipher)) {
$iv_len = $ivLength;
} else {
throw new IvGenerateException();
}

$this->iv = substr($c, 0, $iv_len);
$this->tag = substr($c, $iv_len, static::DEFAULT_GCM_TAG_LENGTH);
$encryptedBytes = substr($c, $iv_len + static::DEFAULT_GCM_TAG_LENGTH);

return openssl_decrypt(
$decryptedText = openssl_decrypt(
$encryptedBytes,
$this->cipher,
$this->key,
Expand All @@ -120,5 +145,11 @@ public function decrypt(string $encryptedData): string
$this->tag,
$this->aad
);

if ($decryptedText == false) {
throw new DecryptException();
}

return $decryptedText;
}
}
2 changes: 2 additions & 0 deletions src/CryptoKey.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ public function helloWorld() : string
}

/**
* Derivate cryptographic key from given password
*
* @param string $password
* @param string|null $salt
* @param int $iterations
Expand Down
13 changes: 13 additions & 0 deletions src/Exceptions/DecryptException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace MayMeow\Cryptography\Exceptions;

use Throwable;

class DecryptException extends \Exception
{
public function __construct(string $message = "Cannot decrypt text", int $code = 0, Throwable $previous = null)
{
parent::__construct($message, $code, $previous);
}
}
16 changes: 16 additions & 0 deletions src/Exceptions/DecryptPrivateKeyException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace MayMeow\Cryptography\Exceptions;

use Throwable;

class DecryptPrivateKeyException extends \Exception
{
public function __construct(
string $message = "Cannot decrypt private key with given password",
int $code = 0,
Throwable $previous = null
) {
parent::__construct($message, $code, $previous);
}
}
13 changes: 13 additions & 0 deletions src/Exceptions/IvGenerateException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace MayMeow\Cryptography\Exceptions;

use Throwable;

class IvGenerateException extends \Exception
{
public function __construct(string $message = "Cannot generate IV", int $code = 0, Throwable $previous = null)
{
parent::__construct($message, $code, $previous);
}
}
13 changes: 13 additions & 0 deletions src/Exceptions/NotImplementedException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace MayMeow\Cryptography\Exceptions;

use Throwable;

class NotImplementedException extends \Exception
{
public function __construct(string $message = "Not Implemented yet!", int $code = 0, Throwable $previous = null)
{
parent::__construct($message, $code, $previous);
}
}
Loading