• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

azjezz / psl / 22639242943

03 Mar 2026 07:30PM UTC coverage: 97.792% (-0.4%) from 98.201%
22639242943

Pull #607

github

azjezz
feat: introduce `Crypto` component

Signed-off-by: azjezz <azjezz@protonmail.com>
Pull Request #607: feat: introduce `Crypto` component

348 of 394 new or added lines in 49 files covered. (88.32%)

2 existing lines in 1 file now uncovered.

9302 of 9512 relevant lines covered (97.79%)

39.03 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

96.3
/src/Psl/Crypto/Aead/decrypt.php
1
<?php
2

3
declare(strict_types=1);
4

5
namespace Psl\Crypto\Aead;
6

7
use Psl\Crypto\Exception;
8
use Psl\Crypto\Internal;
9
use SensitiveParameter;
10

11
use function sodium_crypto_aead_aes256gcm_decrypt;
12
use function sodium_crypto_aead_aes256gcm_is_available;
13
use function sodium_crypto_aead_chacha20poly1305_ietf_decrypt;
14
use function sodium_crypto_aead_xchacha20poly1305_ietf_decrypt;
15

16
/**
17
 * Decrypt a ciphertext message using AEAD with an explicit nonce.
18
 *
19
 * @throws Exception\DecryptionException If decryption fails.
20
 * @throws Exception\RuntimeException If AES-256-GCM is not available.
21
 */
22
function decrypt(
23
    #[SensitiveParameter] string $ciphertext,
24
    #[SensitiveParameter] Key $key,
25
    #[SensitiveParameter] string $nonce,
26
    string $additional_data,
27
    Algorithm $algorithm,
28
): string {
29
    $result = match ($algorithm) {
12✔
30
        Algorithm::Aes256Gcm => (static function () use ($ciphertext, $key, $nonce, $additional_data): string|false {
12✔
31
            if (!sodium_crypto_aead_aes256gcm_is_available()) {
1✔
NEW
32
                throw new Exception\RuntimeException('AES-256-GCM is not available on this platform.');
×
33
            }
34

35
            return Internal\call_sodium(fn() => sodium_crypto_aead_aes256gcm_decrypt(
1✔
36
                $ciphertext,
1✔
37
                $additional_data,
1✔
38
                $nonce,
1✔
39
                $key->bytes,
1✔
40
            ));
1✔
41
        })(),
12✔
42
        Algorithm::XChaCha20Poly1305 => Internal\call_sodium(fn() => sodium_crypto_aead_xchacha20poly1305_ietf_decrypt(
11✔
43
            $ciphertext,
11✔
44
            $additional_data,
11✔
45
            $nonce,
11✔
46
            $key->bytes,
11✔
47
        )),
11✔
48
        Algorithm::ChaCha20Poly1305 => Internal\call_sodium(fn() => sodium_crypto_aead_chacha20poly1305_ietf_decrypt(
3✔
49
            $ciphertext,
3✔
50
            $additional_data,
3✔
51
            $nonce,
3✔
52
            $key->bytes,
3✔
53
        )),
3✔
54
    };
12✔
55

56
    if ($result === false) {
12✔
57
        throw new Exception\DecryptionException('AEAD decryption failed.');
5✔
58
    }
59

60
    return $result;
7✔
61
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc