Skip to content

Commit 6fa2490

Browse files
authored
Merge pull request #1 from neighbourhoodie/v3-v4-folder
Copy over phpseclib3 code to the new repo
2 parents ec7d028 + 9731f81 commit 6fa2490

68 files changed

Lines changed: 2646 additions & 0 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.github/workflows/ci.yml

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
name: phpseclib3 Tests
2+
3+
on: [push]
4+
5+
jobs:
6+
tests:
7+
name: Rector Tests
8+
runs-on: ubuntu-latest
9+
10+
strategy:
11+
fail-fast: false
12+
matrix:
13+
php-version: ['8.3', '8.4', '8.5']
14+
15+
steps:
16+
- name: Checkout
17+
uses: actions/checkout@v4
18+
19+
- name: Setup PHP
20+
uses: shivammathur/setup-php@v2
21+
with:
22+
php-version: ${{ matrix.php-version }}
23+
coverage: none
24+
25+
- name: Install dependencies
26+
run: composer install --no-interaction --prefer-dist
27+
28+
- name: Run Rector tests
29+
run: vendor/bin/phpunit tests

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/composer.lock
2+
/composer.phar
3+
/tests/.phpunit.result.cache
4+
/vendor/

README.md

Lines changed: 200 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,200 @@
1+
# phpseclib3_rector
2+
3+
Rector rules to upgrade a phpseclib v2.0 install to phpseclib v3.0
4+
5+
## Overview
6+
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+
9+
## Installation
10+
11+
With [Composer](https://getcomposer.org/):
12+
13+
```
14+
composer require phpseclib/phpseclib3_rector:~1.0
15+
```
16+
17+
## Usage
18+
19+
Create a rector.php file with the following contents:
20+
21+
```php
22+
<?php
23+
use Rector\Config\RectorConfig;
24+
use phpseclib\phpseclib3Rector\Set;
25+
26+
return RectorConfig::configure()
27+
->withSets([Set::PATH]);
28+
```
29+
In the same directory where you created that file you can then run Rector by doing either of these commands:
30+
31+
```
32+
vendor/bin/rector process src --dry-run
33+
vendor/bin/rector process src
34+
```
35+
The files in the `src/` directory will either be full on modified or (in the case of `--dry-run`) the changes that would be made will be previewed.
36+
37+
## Running the tests
38+
39+
To run all Retor tests, run
40+
41+
```
42+
vendor/bin/phpunit tests
43+
```
44+
45+
To run all tests of a single rector rule, add --filter to the test command.
46+
47+
```
48+
vendor/bin/phpunit tests --filter CustomRectorTest
49+
```
50+
51+
### Test Fixtures
52+
53+
Next to the test case, there is `/Fixture` directory. It contains many test fixture files that verified the Rector rule work correctly in all possible cases.
54+
55+
There are 2 fixture formats:
56+
57+
A. `test_fixture.php.inc` - The Code Should Change
58+
59+
```php
60+
<code before>
61+
-----
62+
<code after>'
63+
```
64+
65+
B. `skip_rule_test_fixture.php.inc` - The Code Should Be Skipped
66+
67+
```php
68+
<code before>
69+
```
70+
71+
## Rules
72+
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->withSaltLength(10) |
108+
| $rsa->setSignatureMode(RSA::SIGNATURE_PKCS1); | $rsa->withPadding(RSA::SIGNATURE_PKCS1); |
109+
| $rsa->setEncryptionMode(RSA::ENCRYPTION_PKCS1); | $rsa->withPadding(RSA::ENCYRPTION_PKCS1); |
110+
| $rsa->setEncryptionMode(RSA::ENCRYPTION_PKCS1); $rsa->setSignatureMode(RSA::SIGNATURE_PKCS1); | $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+
```

composer.json

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{
2+
"name": "phpseclib/phpseclib3_rector",
3+
"description": "Rector rules for upgrading from phpseclib v2 to phpseclib v3",
4+
"type": "rector-extension",
5+
"require": {
6+
"rector/rector": "^2.0.0"
7+
},
8+
"require-dev": {
9+
"phpunit/phpunit": "12.x"
10+
},
11+
"license": "MIT",
12+
"autoload": {
13+
"psr-4": {
14+
"phpseclib\\phpseclib3Rector\\": "src/",
15+
"phpseclib\\phpseclib3Rector\\Tests\\": "tests/"
16+
}
17+
},
18+
"authors": [
19+
{
20+
"name": "terrafrost",
21+
"email": "terrafrost@php.net"
22+
}
23+
]
24+
}

rector.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Rector\Config\RectorConfig;
6+
use phpseclib\phpseclib3Rector\Set;
7+
8+
return RectorConfig::configure()
9+
->withSets([Set::PATH]);

0 commit comments

Comments
 (0)