Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
4 / 4
CRAP
100.00% covered (success)
100.00%
1 / 1
BoxTrait
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
4 / 4
4
100.00% covered (success)
100.00%
1 / 1
 makeKeyPair
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 makeNonce
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 makeSecretKey
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 makePublicKey
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 Exception;
13use SensitiveParameter;
14use SodiumException;
15
16/**
17 * Trait BoxTrait.
18 *
19 * @package crypto
20 */
21trait BoxTrait
22{
23    /**
24     * Makes a keypair.
25     *
26     * @throws SodiumException
27     */
28    public static function makeKeyPair() : string
29    {
30        return \sodium_crypto_box_keypair();
31    }
32
33    /**
34     * Makes a box nonce with the correct length.
35     *
36     * @throws Exception if fail to get random bytes
37     *
38     * @return string
39     */
40    public static function makeNonce() : string
41    {
42        return \random_bytes(\SODIUM_CRYPTO_BOX_NONCEBYTES);
43    }
44
45    /**
46     * Makes the secret key from a keypair.
47     *
48     * @param string $keyPair
49     *
50     * @see BoxTrait::makeKeyPair()
51     *
52     * @throws SodiumException
53     *
54     * @return string
55     */
56    public static function makeSecretKey(#[SensitiveParameter] string $keyPair) : string
57    {
58        return \sodium_crypto_box_secretkey($keyPair);
59    }
60
61    /**
62     * Makes the public key from a keypair.
63     *
64     * @param string $keyPair
65     *
66     * @see BoxTrait::makeKeyPair()
67     *
68     * @throws SodiumException
69     *
70     * @return string
71     */
72    public static function makePublicKey(#[SensitiveParameter] string $keyPair) : string
73    {
74        return \sodium_crypto_box_publickey($keyPair);
75    }
76}