Skip to content

Commit 7b0ef00

Browse files
author
May Meow
authored
Merge pull request #6 from MayMeow/add-phpstan
🔧 add phpstan
2 parents 37b8f53 + e32a803 commit 7b0ef00

12 files changed

+242
-49
lines changed

.github/workflows/php.yml

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,22 @@ jobs:
1717
composer_script: 'test'
1818

1919

20-
qa:
20+
codesniffer:
2121
runs-on: ubuntu-latest
2222
steps:
2323
- uses: actions/checkout@v2
2424
- name: Composer run action PHPC_CS
2525
uses: MayMeowHQ/composer-run-action@v1
2626
with:
2727
composer_script: 'codesniffer'
28+
29+
stan:
30+
runs-on: ubuntu-latest
31+
steps:
32+
- uses: actions/checkout@v2
33+
- name: Composer run action PHPStan
34+
uses: MayMeowHQ/composer-run-action@v1
35+
with:
36+
composer_script: 'stan'
2837

2938

composer.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,12 @@
2222
},
2323
"require-dev": {
2424
"phpunit/phpunit": "^9.5",
25-
"squizlabs/php_codesniffer": "^3.6"
25+
"squizlabs/php_codesniffer": "^3.6",
26+
"phpstan/phpstan": "^0.12.99"
2627
},
2728
"scripts": {
2829
"test": "phpunit tests",
29-
"codesniffer": "phpcs --standard=PSR2 src"
30+
"codesniffer": "phpcs --standard=PSR2 src",
31+
"stan": "phpstan analyse"
3032
}
3133
}

composer.lock

Lines changed: 65 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

phpstan.neon

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
parameters:
2+
level: 7
3+
checkMissingIterableValueType: false
4+
treatPhpDocTypesAsCertain: false
5+
paths:
6+
- src

src/AESCryptoServiceProvider.php

Lines changed: 46 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,25 @@
22

33
namespace MayMeow\Cryptography;
44

5+
use MayMeow\Cryptography\Exceptions\DecryptException;
6+
use MayMeow\Cryptography\Exceptions\IvGenerateException;
7+
58
class AESCryptoServiceProvider
69
{
710
const CIPHER_TYPE_GCM = 'aes-256-gcm';
811
const DEFAULT_GCM_TAG_LENGTH = 16;
912

10-
protected $cipher;
13+
protected string $cipher;
1114

12-
protected $iv;
15+
protected string $iv;
1316

14-
protected $key;
17+
protected string $key;
1518

16-
protected $aad = "127.0.0.1";
19+
protected string $aad = "127.0.0.1";
1720

18-
protected $tag;
21+
protected string $tag = '';
1922

20-
public function __construct($cipher = null)
23+
public function __construct(string $cipher = null)
2124
{
2225
if ($cipher == null) {
2326
$this->cipher = static::CIPHER_TYPE_GCM;
@@ -27,34 +30,42 @@ public function __construct($cipher = null)
2730
}
2831

2932
/**
30-
* @param $iv
33+
* Set IV
34+
*
35+
* @param string $iv
3136
* @return AESCryptoServiceProvider
3237
*/
33-
public function setIV($iv): AESCryptoServiceProvider
38+
public function setIV(string $iv): AESCryptoServiceProvider
3439
{
3540
$this->iv = $iv;
3641

3742
return $this;
3843
}
3944

4045
/**
41-
* @param $key
46+
* Set key needed for encryption
47+
*
48+
* @param string $key
4249
* @return AESCryptoServiceProvider
4350
*/
44-
public function setKey($key): AESCryptoServiceProvider
51+
public function setKey(string $key): AESCryptoServiceProvider
4552
{
4653
$this->key = $key;
4754

4855
return $this;
4956
}
5057

5158
/**
59+
* Generate key
60+
*
5261
* @return bool|string
5362
*/
5463
public function generateKey()
5564
{
5665
if (in_array($this->cipher, openssl_get_cipher_methods())) {
57-
$this->key = openssl_random_pseudo_bytes(32);
66+
if ($key = openssl_random_pseudo_bytes(32)) {
67+
$this->key = $key;
68+
}
5869

5970
return $this->key;
6071
}
@@ -63,12 +74,18 @@ public function generateKey()
6374
}
6475

6576
/**
77+
* Generate IV
78+
*
6679
* @return bool|string
6780
*/
6881
public function generateIV()
6982
{
7083
if (in_array($this->cipher, openssl_get_cipher_methods())) {
71-
$this->iv = openssl_random_pseudo_bytes(openssl_cipher_iv_length($this->cipher));
84+
if ($ivLength = openssl_cipher_iv_length($this->cipher)) {
85+
if ($iv = openssl_random_pseudo_bytes($ivLength)) {
86+
$this->iv = $iv;
87+
}
88+
}
7289

7390
return $this->iv;
7491
}
@@ -98,20 +115,28 @@ public function encrypt(string $plainText): string
98115
}
99116

100117
/**
101-
* Returns decrypted text
118+
* Decrypt given text
119+
*
102120
* @param string $encryptedData
103121
* @return string
122+
* @throws DecryptException
123+
* @throws IvGenerateException
104124
*/
105125
public function decrypt(string $encryptedData): string
106126
{
107127
$c = base64_decode($encryptedData);
108128

109-
$iv_len = openssl_cipher_iv_length($this->cipher);
129+
if ($ivLength = openssl_cipher_iv_length($this->cipher)) {
130+
$iv_len = $ivLength;
131+
} else {
132+
throw new IvGenerateException();
133+
}
134+
110135
$this->iv = substr($c, 0, $iv_len);
111136
$this->tag = substr($c, $iv_len, static::DEFAULT_GCM_TAG_LENGTH);
112137
$encryptedBytes = substr($c, $iv_len + static::DEFAULT_GCM_TAG_LENGTH);
113138

114-
return openssl_decrypt(
139+
$decryptedText = openssl_decrypt(
115140
$encryptedBytes,
116141
$this->cipher,
117142
$this->key,
@@ -120,5 +145,11 @@ public function decrypt(string $encryptedData): string
120145
$this->tag,
121146
$this->aad
122147
);
148+
149+
if ($decryptedText == false) {
150+
throw new DecryptException();
151+
}
152+
153+
return $decryptedText;
123154
}
124155
}

src/CryptoKey.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ public function helloWorld() : string
1111
}
1212

1313
/**
14+
* Derivate cryptographic key from given password
15+
*
1416
* @param string $password
1517
* @param string|null $salt
1618
* @param int $iterations

src/Exceptions/DecryptException.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace MayMeow\Cryptography\Exceptions;
4+
5+
use Throwable;
6+
7+
class DecryptException extends \Exception
8+
{
9+
public function __construct(string $message = "Cannot decrypt text", int $code = 0, Throwable $previous = null)
10+
{
11+
parent::__construct($message, $code, $previous);
12+
}
13+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace MayMeow\Cryptography\Exceptions;
4+
5+
use Throwable;
6+
7+
class DecryptPrivateKeyException extends \Exception
8+
{
9+
public function __construct(
10+
string $message = "Cannot decrypt private key with given password",
11+
int $code = 0,
12+
Throwable $previous = null
13+
) {
14+
parent::__construct($message, $code, $previous);
15+
}
16+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace MayMeow\Cryptography\Exceptions;
4+
5+
use Throwable;
6+
7+
class IvGenerateException extends \Exception
8+
{
9+
public function __construct(string $message = "Cannot generate IV", int $code = 0, Throwable $previous = null)
10+
{
11+
parent::__construct($message, $code, $previous);
12+
}
13+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace MayMeow\Cryptography\Exceptions;
4+
5+
use Throwable;
6+
7+
class NotImplementedException extends \Exception
8+
{
9+
public function __construct(string $message = "Not Implemented yet!", int $code = 0, Throwable $previous = null)
10+
{
11+
parent::__construct($message, $code, $previous);
12+
}
13+
}

0 commit comments

Comments
 (0)