Utils   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Importance

Changes 3
Bugs 1 Features 0
Metric Value
wmc 5
eloc 16
c 3
b 1
f 0
dl 0
loc 46
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A generateSigningKeypair() 0 10 2
A zero() 0 3 1
A generateKeypair() 0 10 2
1
<?php declare(strict_types=1);
2
3
namespace ncryptf;
4
5
use SodiumException;
6
use ncryptf\Keypair;
7
8
final class Utils
9
{
10
    /**
11
     * Securely erases a memory block
12
     *
13
     * @param string $data
14
     * @return boolean
15
     */
16
    public static function zero(string &$data) : bool
17
    {
18
        return \sodium_memzero($data) === null;
0 ignored issues
show
Bug introduced by
Are you sure the usage of sodium_memzero($data) is correct as it seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
19
    }
20
21
    /**
22
     * Generates a crypto keypair
23
     *
24
     * @return \ncryptf\Keypair
25
     */
26
    public static function generateKeypair() : Keypair
27
    {
28
        try {
29
            $keypair = \sodium_crypto_box_keypair();
30
            return new Keypair(
31
                \sodium_crypto_box_secretkey($keypair),
32
                \sodium_crypto_box_publickey($keypair)
33
            );
34
        } catch (SodiumException $e) {
35
            throw new Exception($e->getMessage());
36
        }
37
    }
38
39
    /**
40
     * Generates a signing keypair
41
     *
42
     * @return \ncryptf\Keypair
43
     */
44
    public static function generateSigningKeypair() : Keypair
45
    {
46
        try {
47
            $keypair = \sodium_crypto_sign_keypair();
48
            return new Keypair(
49
                \sodium_crypto_sign_secretkey($keypair),
50
                \sodium_crypto_sign_publickey($keypair)
51
            );
52
        } catch (SodiumException $e) {
53
            throw new Exception($e->getMessage());
54
        }
55
    }
56
}
57