Test_EncryptionKey::getKeyPair()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 6
c 2
b 0
f 0
dl 0
loc 10
rs 10
cc 2
nc 2
nop 1
1
<?php
2
3
namespace LeKoala\Encrypt\Test;
4
5
use SilverStripe\Dev\TestOnly;
6
use ParagonIE\ConstantTime\Hex;
7
use SilverStripe\ORM\DataObject;
8
use SilverStripe\Security\Member;
9
10
/**
11
 * @property string $EncryptionKey
12
 * @property int $MemberID
13
 */
14
class Test_EncryptionKey extends DataObject implements TestOnly
15
{
16
    /**
17
     * @var string
18
     */
19
    private static $table_name = 'EncryptionKey';
0 ignored issues
show
introduced by
The private property $table_name is not used, and could be removed.
Loading history...
20
21
    /**
22
     * @var array<string,string>
23
     */
24
    private static $db = [
0 ignored issues
show
introduced by
The private property $db is not used, and could be removed.
Loading history...
25
        // A 256 bit key (32 bytes = 32*8)
26
        // 32 bytes = 64 chars in hex (eg using bin2hex)
27
        "EncryptionKey" => 'Varchar',
28
29
        // X25519 Keypair (2x 32 bytes keys)
30
        "SecretKey" => 'Varchar',
31
        "PublicKey" => 'Varchar',
32
    ];
33
34
    /**
35
     * @var array<string,string>
36
     */
37
    private static $has_one = [
0 ignored issues
show
introduced by
The private property $has_one is not used, and could be removed.
Loading history...
38
        "Member" => Member::class,
39
    ];
40
41
    public static function getForMember(int $ID): ?string
42
    {
43
        $rec = self::get()->filter('MemberID', $ID)->first();
44
        return $rec->EncryptionKey ?? null;
45
    }
46
47
    /**
48
     * @param integer $ID
49
     * @return array<string,string>|null
50
     */
51
    public static function getKeyPair(int $ID): ?array
52
    {
53
        $rec = self::get()->filter('MemberID', $ID)->first();
54
        if ($rec) {
55
            return [
56
                'public' => Hex::decode($rec->PublicKey),
57
                'secret' => Hex::decode($rec->SecretKey),
58
            ];
59
        }
60
        return null;
61
    }
62
}
63