Skip to content

Commit 18048c1

Browse files
Merge pull request #172 from paragonie/v2-init
Begin New Major Version (2.0.0)
2 parents 102bcf2 + 0a16d0a commit 18048c1

File tree

119 files changed

+3518
-22526
lines changed

Some content is hidden

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

119 files changed

+3518
-22526
lines changed

.gitattributes

+1-3
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,11 @@
33
/.github export-ignore
44
/phpunit.xml.dist export-ignore
55
/phpstan.neon.dist export-ignore
6-
/psalm-above-3.xml export-ignore
7-
/psalm-below-3.xml export-ignore
6+
/psalm.xml export-ignore
87
/tests export-ignore
98
/build-phar.sh export-ignore
109
/appveyor.yml export-ignore
1110
/dist export-ignore
1211
/autoload-phpunit.php export-ignore
1312
/autoload-pedantic.php export-ignore
1413
/autoload-fast.php export-ignore
15-

.github/workflows/ci.yml

+2-31
Original file line numberDiff line numberDiff line change
@@ -3,46 +3,17 @@ name: CI
33
on: [push]
44

55
jobs:
6-
old:
7-
name: PHP ${{ matrix.php-versions }} Test on ${{ matrix.operating-system }}
8-
runs-on: ${{ matrix.operating-system }}
9-
strategy:
10-
fail-fast: false
11-
matrix:
12-
operating-system: ['ubuntu-latest']
13-
php-versions: ['5.3', '5.4', '5.5', '5.6', '7.0']
14-
steps:
15-
- name: Checkout
16-
uses: actions/checkout@v4
17-
18-
- name: Setup PHP
19-
uses: shivammathur/setup-php@v2
20-
with:
21-
php-version: ${{ matrix.php-versions }}
22-
extensions: mbstring, intl
23-
ini-values: max_execution_time=600, memory_limit=256M, error_reporting=-1, display_errors=On
24-
coverage: none
25-
26-
- name: Use Composer 1.x
27-
run: composer self-update --1
28-
29-
- name: Install Composer dependencies
30-
uses: "ramsey/composer-install@v2"
31-
32-
- name: PHPUnit tests
33-
run: vendor/bin/phpunit
34-
356
moderate-modern:
367
name: PHP ${{ matrix.php-versions }} Test on ${{ matrix.operating-system }}
378
runs-on: ${{ matrix.operating-system }}
389
strategy:
3910
matrix:
4011
operating-system: ['ubuntu-latest']
41-
php-versions: ['7.1', '7.2', '7.3', '7.4', '8.0', '8.1', '8.2', '8.3', '8.4']
12+
php-versions: ['8.1', '8.2', '8.3', '8.4']
4213

4314
steps:
4415
- name: Checkout
45-
uses: actions/checkout@v3
16+
uses: actions/checkout@v4
4617

4718
- name: Setup PHP
4819
uses: shivammathur/setup-php@v2

.github/workflows/psalm.yml

+1-4
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ jobs:
99
strategy:
1010
matrix:
1111
operating-system: ['ubuntu-latest']
12-
php-versions: ['7.4']
12+
php-versions: ['8.3']
1313
steps:
1414
- name: Checkout
1515
uses: actions/checkout@v4
@@ -26,8 +26,5 @@ jobs:
2626
with:
2727
composer-options: --no-dev
2828

29-
- name: Put Psalm config in place
30-
run: cp psalm-above-3.xml psalm.xml
31-
3229
- name: Static Analysis
3330
run: psalm

.gitignore

-1
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,3 @@
77
/composer.lock
88
/composer.phar
99
/vendor/
10-
/psalm.xml

LICENSE

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
ISC License
22

3-
Copyright (c) 2016-2023, Paragon Initiative Enterprises <security at paragonie dot com>
3+
Copyright (c) 2016-2024, Paragon Initiative Enterprises <security at paragonie dot com>
44
Copyright (c) 2013-2019, Frank Denis <j at pureftpd dot org>
55

66
Permission to use, copy, modify, and/or distribute this software for any

README.md

+4-51
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
[![Build Status](https://github.com/paragonie/sodium_compat/actions/workflows/ci.yml/badge.svg)](https://github.com/paragonie/sodium_compat/actions)
44
[![Psalm Status](https://github.com/paragonie/sodium_compat/actions/workflows/psalm.yml/badge.svg)](https://github.com/paragonie/sodium_compat/actions)
5-
[![Windows Build Status](https://ci.appveyor.com/api/projects/status/itcx1vgmfqiawgbe?svg=true)](https://ci.appveyor.com/project/paragonie-scott/sodium-compat)
65
[![Latest Stable Version](https://poser.pugx.org/paragonie/sodium_compat/v/stable)](https://packagist.org/packages/paragonie/sodium_compat)
76
[![Latest Unstable Version](https://poser.pugx.org/paragonie/sodium_compat/v/unstable)](https://packagist.org/packages/paragonie/sodium_compat)
87
[![License](https://poser.pugx.org/paragonie/sodium_compat/license)](https://packagist.org/packages/paragonie/sodium_compat)
@@ -17,8 +16,8 @@ and transparently use the PHP extension instead of our implementation.
1716
## Major Versions and Branches
1817

1918
sodium_compat v1.21.0 was the last v1.x release from the master branch. From now
20-
on, all future releases that support PHP 5.2 - 5.6 and 32-bit integers will be
21-
[in the `v1.x` branch](v1.x).
19+
on, all future releases that support PHP 5.2 - 8.0 and 32-bit integers will be
20+
[in the `v1.x` branch](https://github.com/paragonie/sodium_compat/tree/v1.x).
2221

2322
Newer versions of sodium_compat (i.e., v2.0.0) will continue to live in the master
2423
branch, unless a new major version is needed. The goal of this work is to improve
@@ -32,7 +31,7 @@ for the latest in version information.
3231
| sodium_compat version | PHP versions supported | 32-bit support? | Branch |
3332
|-----------------------|------------------------|-----------------|---------------------------------------------------------------|
3433
| `v1.x.y` | 5.2.4 - LATEST | YES | [v1.x](https://github.com/paragonie/sodium_compat/tree/v1.x) |
35-
| `v2.x.y` | 7.2 - LATEST | NO | **master** |
34+
| `v2.x.y` | 8.1 - LATEST | NO | **master** |
3635

3736
If you need 32-bit PHP support (`PHP_INT_SIZE == 4`), continue using sodium_compat v1.x.
3837
If you want improved performance and smaller dependencies, use v2.x.
@@ -161,32 +160,6 @@ if (sodium_crypto_sign_verify_detached($signature, $message, $alice_pk)) {
161160
}
162161
```
163162

164-
## Polyfill For the Old PECL Extension API
165-
166-
If you're using PHP 5.3.0 or newer and do not have the PECL extension installed,
167-
you can just use the [standard ext/sodium API features as-is](https://paragonie.com/book/pecl-libsodium)
168-
and the polyfill will work its magic.
169-
170-
```php
171-
<?php
172-
require_once "/path/to/sodium_compat/autoload.php";
173-
174-
$alice_kp = \Sodium\crypto_sign_keypair();
175-
$alice_sk = \Sodium\crypto_sign_secretkey($alice_kp);
176-
$alice_pk = \Sodium\crypto_sign_publickey($alice_kp);
177-
178-
$message = 'This is a test message.';
179-
$signature = \Sodium\crypto_sign_detached($message, $alice_sk);
180-
if (\Sodium\crypto_sign_verify_detached($signature, $message, $alice_pk)) {
181-
echo 'OK', PHP_EOL;
182-
} else {
183-
throw new Exception('Invalid signature');
184-
}
185-
```
186-
187-
The polyfill does not expose this API on PHP < 5.3, or if you have the PHP
188-
extension installed already.
189-
190163
## General-Use Polyfill
191164

192165
If your users are on PHP < 5.3, or you want to write code that will work
@@ -212,7 +185,7 @@ if (ParagonIE_Sodium_Compat::crypto_sign_verify_detached($signature, $message, $
212185
}
213186
```
214187

215-
Generally: If you replace `\Sodium\ ` with `ParagonIE_Sodium_Compat::`, any
188+
Generally: If you replace `sodium_` with `ParagonIE_Sodium_Compat::`, any
216189
code already written for the libsodium PHP extension should work with our
217190
polyfill without additional code changes.
218191

@@ -254,26 +227,6 @@ if (ParagonIE_Sodium_Compat::polyfill_is_fast()) {
254227
}
255228
```
256229

257-
### Help, my PHP only has 32-Bit Integers! It's super slow!
258-
259-
If the `PHP_INT_SIZE` constant equals `4` instead of `8` (PHP 5 on Windows,
260-
Linux on i386, etc.), you will run into **significant performance issues**.
261-
262-
In particular: public-key cryptography (encryption and signatures)
263-
is affected. There is nothing we can do about that.
264-
265-
The root cause of these performance issues has to do with implementing cryptography
266-
algorithms in constant-time using 16-bit limbs (to avoid overflow) in pure PHP.
267-
268-
To mitigate these performance issues, simply install PHP 7.2 or newer and enable
269-
the `sodium` extension.
270-
271-
Affected users are encouraged to install the sodium extension (or libsodium from
272-
older version of PHP).
273-
274-
Windows users on PHP 5 may be able to simply upgrade to PHP 7 and the slowdown
275-
will be greatly reduced.
276-
277230
## Documentation
278231

279232
First, you'll want to read the [Libsodium Quick Reference](https://paragonie.com/blog/2017/06/libsodium-quick-reference-quick-comparison-similar-functions-and-which-one-use).

appveyor.yml

-28
This file was deleted.

autoload-php7.php

-31
This file was deleted.

autoload.php

+28-54
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,28 @@
11
<?php
22

3-
if (PHP_VERSION_ID < 70000) {
4-
if (!is_callable('sodiumCompatAutoloader')) {
5-
/**
6-
* Sodium_Compat autoloader.
7-
*
8-
* @param string $class Class name to be autoloaded.
9-
*
10-
* @return bool Stop autoloading?
11-
*/
12-
function sodiumCompatAutoloader($class)
13-
{
14-
$namespace = 'ParagonIE_Sodium_';
15-
// Does the class use the namespace prefix?
16-
$len = strlen($namespace);
17-
if (strncmp($namespace, $class, $len) !== 0) {
18-
// no, move to the next registered autoloader
19-
return false;
20-
}
21-
22-
// Get the relative class name
23-
$relative_class = substr($class, $len);
3+
spl_autoload_register(function ($class) {
4+
$namespace = 'ParagonIE_Sodium_';
5+
// Does the class use the namespace prefix?
6+
$len = strlen($namespace);
7+
if (strncmp($namespace, $class, $len) !== 0) {
8+
// no, move to the next registered autoloader
9+
return false;
10+
}
2411

25-
// Replace the namespace prefix with the base directory, replace namespace
26-
// separators with directory separators in the relative class name, append
27-
// with .php
28-
$file = dirname(__FILE__) . '/src/' . str_replace('_', '/', $relative_class) . '.php';
29-
// if the file exists, require it
30-
if (file_exists($file)) {
31-
require_once $file;
32-
return true;
33-
}
34-
return false;
35-
}
12+
// Get the relative class name
13+
$relative_class = substr($class, $len);
3614

37-
// Now that we have an autoloader, let's register it!
38-
spl_autoload_register('sodiumCompatAutoloader');
15+
// Replace the namespace prefix with the base directory, replace namespace
16+
// separators with directory separators in the relative class name, append
17+
// with .php
18+
$file = dirname(__FILE__) . '/src/' . str_replace('_', '/', $relative_class) . '.php';
19+
// if the file exists, require it
20+
if (file_exists($file)) {
21+
require_once $file;
22+
return true;
3923
}
40-
} else {
41-
require_once dirname(__FILE__) . '/autoload-php7.php';
42-
}
24+
return false;
25+
});
4326

4427
/* Explicitly, always load the Compat class: */
4528
if (!class_exists('ParagonIE_Sodium_Compat', false)) {
@@ -49,26 +32,17 @@ function sodiumCompatAutoloader($class)
4932
if (!class_exists('SodiumException', false)) {
5033
require_once dirname(__FILE__) . '/src/SodiumException.php';
5134
}
52-
if (PHP_VERSION_ID >= 50300) {
53-
// Namespaces didn't exist before 5.3.0, so don't even try to use this
54-
// unless PHP >= 5.3.0
55-
require_once dirname(__FILE__) . '/lib/namespaced.php';
56-
require_once dirname(__FILE__) . '/lib/sodium_compat.php';
57-
if (!defined('SODIUM_CRYPTO_AEAD_AEGIS128L_KEYBYTES')) {
58-
require_once dirname(__FILE__) . '/lib/php84compat_const.php';
59-
}
60-
} else {
61-
require_once dirname(__FILE__) . '/src/PHP52/SplFixedArray.php';
35+
36+
require_once dirname(__FILE__) . '/lib/namespaced.php';
37+
if (!defined('SODIUM_CRYPTO_AEAD_AEGIS128L_KEYBYTES')) {
38+
require_once dirname(__FILE__) . '/lib/php84compat_const.php';
6239
}
63-
if (PHP_VERSION_ID < 70200 || !extension_loaded('sodium')) {
64-
if (PHP_VERSION_ID >= 50300 && !defined('SODIUM_CRYPTO_SCALARMULT_BYTES')) {
40+
41+
if (!extension_loaded('sodium')) {
42+
if (!defined('SODIUM_CRYPTO_SCALARMULT_BYTES')) {
6543
require_once dirname(__FILE__) . '/lib/php72compat_const.php';
6644
}
67-
if (PHP_VERSION_ID >= 70000) {
68-
assert(class_exists('ParagonIE_Sodium_Compat'), 'Possible filesystem/autoloader bug?');
69-
} else {
70-
assert(class_exists('ParagonIE_Sodium_Compat'));
71-
}
45+
assert(class_exists('ParagonIE_Sodium_Compat'), 'Possible filesystem/autoloader bug?');
7246
require_once(dirname(__FILE__) . '/lib/php72compat.php');
7347
} elseif (!function_exists('sodium_crypto_stream_xchacha20_xor')) {
7448
// Older versions of {PHP, ext/sodium} will not define these

0 commit comments

Comments
 (0)