Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
BoxSeal
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
2 / 2
2
100.00% covered (success)
100.00%
1 / 1
 encrypt
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 decrypt
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
1<?php declare(strict_types=1);
2/*
3 * This file is part of Aplus Framework Crypto Library.
4 *
5 * (c) Natan Felles <natanfelles@gmail.com>
6 *
7 * For the full copyright and license information, please view the LICENSE
8 * file that was distributed with this source code.
9 */
10namespace Framework\Crypto;
11
12use SensitiveParameter;
13use SodiumException;
14
15/**
16 * Class BoxSeal.
17 *
18 * @package crypto
19 */
20class BoxSeal
21{
22    use BoxTrait;
23
24    /**
25     * Encrypts a message with an anonymous public key.
26     *
27     * @param string $message
28     * @param string $publicKey
29     *
30     * @see BoxTrait::makePublicKey()
31     *
32     * @throws SodiumException
33     *
34     * @return string
35     */
36    public static function encrypt(
37        #[SensitiveParameter] string $message,
38        #[SensitiveParameter] string $publicKey
39    ) : string {
40        return \sodium_crypto_box_seal($message, $publicKey);
41    }
42
43    /**
44     * Decrypts a message ciphertext.
45     *
46     * @param string $ciphertext
47     * @param string $keyPair
48     *
49     * @see BoxTrait::makeKeyPair()
50     *
51     * @throws SodiumException
52     *
53     * @return false|string The message or false if the ciphertext could not be
54     * decrypted
55     */
56    public static function decrypt(
57        #[SensitiveParameter] string $ciphertext,
58        #[SensitiveParameter] string $keyPair
59    ) : false | string {
60        return \sodium_crypto_box_seal_open($ciphertext, $keyPair);
61    }
62}