Passed
Push — master ( 883646...00eb2e )
by Charles
02:59
created

Response::isSignatureValid()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 7
nc 2
nop 3
dl 0
loc 10
rs 10
c 0
b 0
f 0
1
<?php declare(strict_types=1);
2
3
namespace ncryptf;
4
5
use InvalidArgumentException;
6
use SodiumException;
7
8
class Response
9
{
10
    /**
11
     * Sodium CryptoBox Keypair
12
     *
13
     * @var string
14
     */
15
    private $keypair;
16
17
    /**
18
     * Constructor
19
     * 
20
     * @param string $secretKey The 32 byte secret key
21
     * @param string $publicKey The 32 byte public key
22
     */
23
    public function __construct(string $secretKey, string $publicKey)
24
    {
25
        try {
26
            $this->keypair = \sodium_crypto_box_keypair_from_secretkey_and_publickey(
27
                $secretKey,
28
                $publicKey
29
            );
30
        } catch (SodiumException $e) {
31
            throw new InvalidArgumentException($e->getMessage());
32
        }
33
    }
34
35
    /**
36
     * Decrypts a given response with a nonce
37
     * This will return the decrypted string of decrypt was successful, and false otherwise
38
     * 
39
     * @param string $response  The encrypted HTTP response, as a multi-byte string
40
     * @param string $nonce     The 32 byte nonce
41
     * @return string|bool
42
     */
43
    public function decrypt(string $response, string $nonce)
44
    {
45
        try {
46
            return \sodium_crypto_box_open(
47
                $response,
48
                $nonce,
49
                $this->keypair
50
            );
51
        } catch (SodiumException $e) {
52
            throw new InvalidArguementException($e->getMessage());
0 ignored issues
show
Bug introduced by
The type ncryptf\InvalidArguementException was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
53
        }
54
    }
55
56
    /**
57
     * Returns true if the signature validates the response
58
     * 
59
     * @param string $response  The raw http response, after decoding
60
     * @param string $signature The raw multi-byte signature
61
     * @param string $publicKey The signing public key
62
     * @return bool
63
     */
64
    public function isSignatureValid(string $response, string $signature, string $publicKey)
65
    {
66
        try {
67
            return \sodium_crypto_sign_verify_detached(
68
                $signature,
69
                $response,
70
                $publicKey
71
            );
72
        } catch (SodiumException $e) {
73
            throw new InvalidArguementException($e->getMessage());
74
        }
75
    }
76
}