Skip to content

Commit 9da7b02

Browse files
authored
Merge pull request #4 from neighbourhoodie/v3-to-v4-updates
V3 to v4 updates
2 parents c8ed87f + 95fc7b8 commit 9da7b02

39 files changed

Lines changed: 1639 additions & 172 deletions

.github/workflows/ci.yml

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
name: phpseclib3 Tests
1+
name: Rector rules Tests
22

33
on: [push]
44

@@ -25,5 +25,8 @@ jobs:
2525
- name: Install dependencies
2626
run: composer install --no-interaction --prefer-dist
2727

28-
- name: Run Rector tests
29-
run: vendor/bin/phpunit tests
28+
- name: Run Rector tests for 2-to-3 upgrade
29+
run: vendor/bin/phpunit tests --filter V2toV3
30+
31+
- name: Run Rector tests for 3-to-4 upgrade
32+
run: vendor/bin/phpunit tests --filter V3toV4

README.md

Lines changed: 32 additions & 168 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,29 @@
11
# rector_rules
22

3-
Rector rules to upgrade a phpseclib v2.0 install to phpseclib v3.0
3+
Rector rules to upgrade a phpseclib v2.0 install to phpseclib v3.0 or
4+
to upgrade a phpseclib v3.0 install to phpseclib v4.0.
45

56
## Overview
67

7-
You can use [phpseclib2_compat](https://github.com/phpseclib/phpseclib2_compat) to make all your phpseclib v2.0 calls use phpseclib v3.0, internally, under the hood, or you can use this [Rector](https://getrector.com/) rule to upgrade your phpseclib v2.0 calls to phpseclib v3.0 calls.
8+
You can use [phpseclib2_compat](https://github.com/phpseclib/phpseclib2_compat) to make all your phpseclib v2.0 calls use phpseclib v3.0, internally, under the hood.
9+
Or you can use this [Rector](https://getrector.com/) rule to upgrade your
10+
phpseclib v2.0 calls to phpseclib v3.0 calls or your
11+
phpseclib v3.0 calls to your phpseclib v4.0 calls.
812

913
## Installation
1014

1115
With [Composer](https://getcomposer.org/):
1216

13-
```
17+
```bash
1418
composer require phpseclib/rector_rules:~1.0
1519
```
1620

1721
## Usage
1822

1923
Create a rector.php file with the following contents:
2024

25+
### v2 to v3 upgrade
26+
2127
```php
2228
<?php
2329
use Rector\Config\RectorConfig;
@@ -26,6 +32,20 @@ use phpseclib\rectorRules\Set\V2toV3Set;
2632
return RectorConfig::configure()
2733
->withSets([V2toV3Set::PATH]);
2834
```
35+
36+
### v3 to v4 upgrade
37+
38+
```php
39+
<?php
40+
use Rector\Config\RectorConfig;
41+
use phpseclib\rectorRules\Set\V3toV4Set;
42+
43+
return RectorConfig::configure()
44+
->withSets([V3toV4Set::PATH]);
45+
```
46+
47+
### Refactor
48+
2949
In the same directory where you created that file you can then run Rector by doing either of these commands:
3050

3151
```
@@ -38,13 +58,18 @@ The files in the `src/` directory will either be full on modified or (in the cas
3858

3959
To run all Retor tests, run
4060

41-
```
61+
```bash
4262
vendor/bin/phpunit tests
4363
```
4464

65+
To run all tests of a ruleset, add the name of it, like
66+
```bash
67+
vendor/bin/phpunit tests --filter V2toV3
68+
```
69+
4570
To run all tests of a single rector rule, add --filter to the test command.
4671

47-
```
72+
```bash
4873
vendor/bin/phpunit tests --filter CustomRectorTest
4974
```
5075

@@ -59,7 +84,7 @@ A. `test_fixture.php.inc` - The Code Should Change
5984
```php
6085
<code before>
6186
-----
62-
<code after>'
87+
<code after>
6388
```
6489

6590
B. `skip_rule_test_fixture.php.inc` - The Code Should Be Skipped
@@ -70,165 +95,4 @@ B. `skip_rule_test_fixture.php.inc` - The Code Should Be Skipped
7095

7196
## Rules
7297

73-
### Public Key Loader
74-
75-
This rule is for `v2` -> `v3` upgrade.
76-
77-
This rule helps to load unencrypted and encrypted public / private keys.
78-
79-
In `v2` `loadKey()` returns true on success and false on failure. `v2` only supports RSA keys and `$rsa` is *not* immutable.
80-
And in `v3` `PublicKeyLoader` returns an immutable instance of either `\phpseclib3\Crypt\Common\PublicKey` or
81-
`\phpseclib3\Crypt\Common\PrivateKey`. An exception is thrown on failure.
82-
83-
It replaces
84-
```php
85-
use phpseclib\Crypt\RSA;
86-
87-
$rsa = new RSA();
88-
$rsa->loadKey('...');
89-
```
90-
with
91-
92-
```php
93-
use phpseclib3\Crypt\PublicKeyLoader;
94-
95-
$rsa = PublicKeyLoader::load('...');
96-
```
97-
98-
When `setPassword` is used, `$rsa->setPassword('password');` will be replaced with `$rsa = PublicKeyLoader::load('...', $password);`.
99-
100-
Additionally it replaces the following methods:
101-
102-
| v2 | v3 |
103-
|-------------------------------|---------------------------------------|
104-
| $rsa->getSize() | $rsa->getLength() |
105-
| $rsa->setHash('sha256'); | $rsa = $rsa->withHash('sha256') |
106-
| $rsa->setMGFHash('sha256'); | $rsa = $rsa->withMGFHash('sha256') |
107-
| $rsa->setSaltLength(10); | $rsa = $rsa->withSaltLength(10) |
108-
| $rsa->setSignatureMode(RSA::SIGNATURE_PKCS1); | $rsa = $rsa->withPadding(RSA::SIGNATURE_PKCS1); |
109-
| $rsa->setEncryptionMode(RSA::ENCRYPTION_PKCS1); | $rsa = $rsa->withPadding(RSA::ENCYRPTION_PKCS1); |
110-
| $rsa->setEncryptionMode(RSA::ENCRYPTION_PKCS1); $rsa->setSignatureMode(RSA::SIGNATURE_PKCS1); | $rsa = $rsa->withPadding(RSA::SIGNATURE_PKCS1 | RSA::ENCYRPTION_PKCS1);|
111-
112-
### Public Key Loader Chained
113-
114-
Does the same things as `PublicKeyLoader`, but chains the methods.
115-
116-
The chaining option writes everything into _one_ line.
117-
You can format them by running:
118-
119-
```sh
120-
vendor/bin/php-cs-fixer fix path/to/file.php --rules=binary_operator_spaces,method_chaining_indentation
121-
```
122-
123-
### Create Key
124-
125-
This rule is for `v2` -> `v3` upgrade.
126-
127-
It replaces
128-
```php
129-
use phpseclib\Crypt\RSA;
130-
131-
$rsa = new RSA();
132-
extract($rsa->createKey(2048));
133-
```
134-
135-
with
136-
137-
```php
138-
use phpseclib3\Crypt\RSA;
139-
140-
$privateKey = RSA::createKey(2048);
141-
$publicKey = $privateKey->getPublicKey();
142-
$privateKey = (string) $privateKey;
143-
$publicKey = (string) $publicKey;
144-
```
145-
146-
In `v2`, `$rsa->createKey()` returns an array with 3x parameters: `privatekey`, `publickey`, `partial`.
147-
`privatekey` and `public key` are strings, partial can be ignored.
148-
149-
The above `v3` example returns an immutable instance of `phpseclib3\Crypt\Common\PrivateKey`.
150-
151-
The public key can be extracted by: `$rsa->getPublicKey()`.
152-
153-
### HashLength
154-
155-
This rule is for `v2` -> `v3` upgrade.
156-
157-
In phpseclib `v2` getLength would sometimes return the length in bits and sometimes it'd return the length in bytes
158-
in phpseclib `v3` it was made consistent -
159-
getLength always returns the length in bits and getLengthInBytes always returns the length in bytes.
160-
161-
It replaces
162-
```php
163-
use phpseclib\Crypt\Hash;
164-
165-
$hash = new Hash('sha512/224');
166-
echo $hash->getLength();
167-
```
168-
with
169-
```php
170-
use phpseclib3\Crypt\Hash;
171-
172-
$hash = new Hash('sha512/224');
173-
echo $hash->getLengthInBytes();
174-
```
175-
176-
### SFTP File Size
177-
178-
This rule is for `v2` -> `v3` upgrade.
179-
180-
In phpseclib `v2` you have filemtime, fileatime, fileowner but, instead of filesize, you just have size?
181-
phpseclib `v3` fixes that.
182-
183-
It replaces
184-
```php
185-
use phpseclib\Net\SFTP;
186-
187-
$sftp = new SFTP('...');
188-
$sftp->login('username', 'password');
189-
echo $sftp->size('/path/to/filename.ext');
190-
```
191-
192-
with
193-
194-
```php
195-
use phpseclib3\Net\SFTP;
196-
197-
$sftp = new SFTP('...');
198-
$sftp->login('username', 'password');
199-
echo $sftp->filesize('/path/to/filename.ext');
200-
```
201-
202-
### Symmetric Key Constructor
203-
204-
This rule is for `v2` -> `v3` upgrade.
205-
206-
In phpseclib v2 you'd instantiate the constructor by doing stuff like this:
207-
208-
```php
209-
$cipher = new AES(AES::MODE_CTR);
210-
```
211-
212-
In phpseclib v3 that's been replaced with strings. eg.
213-
214-
```php
215-
$cipher = new AES('ctr');
216-
```
217-
218-
Also, in phpseclib v2, `$cipher = new AES()` was the same as `$cipher = new AES(AES::MODE_CBC)`.
219-
In v3 it doesn't default to cbc - the mode needs to be explicitly defined.
220-
221-
222-
This is true for all the classes that extend `\phpseclib3\Crypt\Common\BlockCipher` in v3:
223-
224-
| v2 | v3 |
225-
|---------------------------------------------------|---------------------------------------|
226-
| $default = new DES(); | $default = new DES('cbc'); |
227-
| $des = new DES(DES::MODE_CBC); | $des = new DES('cbc'); |
228-
| $rijndael = new Rijndael(Rijndael::MODE_ECB); | $rijndael = new Rijndael('ecb'); |
229-
| $tripleDES = new TripleDES(TripleDES::MODE_CTR); | $tripleDES = new TripleDES('ctr'); |
230-
| $blowfish = new Blowfish(Blowfish::MODE_CFB); | $blowfish = new Blowfish('cfb'); |
231-
| $twofish = new Twofish(Twofish::MODE_CFB8); | $twofish = new Twofish('cfb8'); |
232-
| $rc2 = new RC2(RC2::MODE_OFB); | $rc2 = new RC2('ofb'); |
233-
| $aes = new AES(AES::MODE_OFB8); | $aes = new AES('ofb8'); |
234-
| $aes2 = new AES(AES::MODE_GCM); | $aes2 = new AES('gcm'); |
98+
Details of the rules are in separate Readme files for [phpseclib v2.0 to phpseclib v3.0](./src/Rector/V2toV3/README.md) and [phpseclib v3.0 to phpseclib v4.0](./src/Rector/V3toV4/README.md).

config/v3-to-v4.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
use Rector\Config\RectorConfig;
3+
4+
use phpseclib\rectorRules\Rector\V3toV4\X509NodeVisitor;
5+
use phpseclib\rectorRules\Rector\V3toV4\X509;
6+
7+
return RectorConfig::configure()
8+
->registerDecoratingNodeVisitor(X509NodeVisitor::class)
9+
->withRules([
10+
X509::class,
11+
])
12+
->withPreparedSets(
13+
codingStyle: true,
14+
);

rector.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
use Rector\Config\RectorConfig;
66
use phpseclib\rectorRules\Set\V2toV3Set;
7+
use phpseclib\rectorRules\Set\V3toV4Set;
78

89
return RectorConfig::configure()
9-
->withSets([V2toV3Set::PATH]);
10+
// ->withSets([V2toV3Set::PATH]);
11+
->withSets([V3toV4Set::PATH]);

0 commit comments

Comments
 (0)