Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
5 / 5 |
|
100.00% |
5 / 5 |
CRAP | |
100.00% |
1 / 1 |
| Sign | |
100.00% |
5 / 5 |
|
100.00% |
5 / 5 |
5 | |
100.00% |
1 / 1 |
| makeKeyPair | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| makeSecretKey | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| makePublicKey | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| signature | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| verify | |
100.00% |
1 / 1 |
|
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 | */ |
| 10 | namespace Framework\Crypto; |
| 11 | |
| 12 | use SensitiveParameter; |
| 13 | use SodiumException; |
| 14 | |
| 15 | /** |
| 16 | * Class Sign. |
| 17 | * |
| 18 | * @package crypto |
| 19 | */ |
| 20 | class Sign |
| 21 | { |
| 22 | /** |
| 23 | * Makes a keypair. |
| 24 | * |
| 25 | * @throws SodiumException |
| 26 | * |
| 27 | * @return string |
| 28 | */ |
| 29 | public static function makeKeyPair() : string |
| 30 | { |
| 31 | return \sodium_crypto_sign_keypair(); |
| 32 | } |
| 33 | |
| 34 | /** |
| 35 | * Makes the secret key from a keypair. |
| 36 | * |
| 37 | * @param string $keyPair |
| 38 | * |
| 39 | * @see Sign::makeKeyPair() |
| 40 | * |
| 41 | * @throws SodiumException |
| 42 | * |
| 43 | * @return string |
| 44 | */ |
| 45 | public static function makeSecretKey(#[SensitiveParameter] string $keyPair) : string |
| 46 | { |
| 47 | return \sodium_crypto_sign_secretkey($keyPair); // @phpstan-ignore-line |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * Makes the public key from a keypair. |
| 52 | * |
| 53 | * @param string $keyPair |
| 54 | * |
| 55 | * @see Sign::makeKeyPair() |
| 56 | * |
| 57 | * @throws SodiumException |
| 58 | * |
| 59 | * @return string |
| 60 | */ |
| 61 | public static function makePublicKey(#[SensitiveParameter] string $keyPair) : string |
| 62 | { |
| 63 | return \sodium_crypto_sign_publickey($keyPair); // @phpstan-ignore-line |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * Gets the digital signature (detached) from a message with a secret key. |
| 68 | * |
| 69 | * @param string $message |
| 70 | * @param string $secretKey |
| 71 | * |
| 72 | * @see Sign::makeSecretKey() |
| 73 | * |
| 74 | * @throws SodiumException |
| 75 | * |
| 76 | * @return string |
| 77 | */ |
| 78 | public static function signature( |
| 79 | #[SensitiveParameter] string $message, |
| 80 | #[SensitiveParameter] string $secretKey |
| 81 | ) : string { |
| 82 | return \sodium_crypto_sign_detached($message, $secretKey); // @phpstan-ignore-line |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * Verifies if a message has a valid signature. |
| 87 | * |
| 88 | * @param string $message |
| 89 | * @param string $signature |
| 90 | * @param string $publicKey |
| 91 | * |
| 92 | * @see Sign::makePublicKey() |
| 93 | * @see Sign::signature() |
| 94 | * |
| 95 | * @throws SodiumException |
| 96 | * |
| 97 | * @return bool |
| 98 | */ |
| 99 | public static function verify( |
| 100 | #[SensitiveParameter] string $message, |
| 101 | #[SensitiveParameter] string $signature, |
| 102 | #[SensitiveParameter] string $publicKey |
| 103 | ) : bool { |
| 104 | return \sodium_crypto_sign_verify_detached($signature, $message, $publicKey); // @phpstan-ignore-line |
| 105 | } |
| 106 | } |