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
![]() |
|||
20 | |||
21 | /** |
||
22 | * @var array<string,string> |
||
23 | */ |
||
24 | private static $db = [ |
||
0 ignored issues
–
show
|
|||
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
|
|||
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 |