Passed
Pull Request — master (#6)
by Chris
05:44
created

HaliteEncryptor::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Carnage\EncryptedColumn\Encryptor;
4
5
use Carnage\EncryptedColumn\ValueObject\EncryptorIdentity;
6
use Carnage\EncryptedColumn\ValueObject\IdentityInterface;
7
use Carnage\EncryptedColumn\ValueObject\Key;
8
use ParagonIE\Halite\KeyFactory;
9
use ParagonIE\Halite\Symmetric;
10
11
class HaliteEncryptor implements EncryptorInterface
12
{
13
    const IDENTITY = 'halite';
14
15 6
    public function encrypt($data, Key $key)
16
    {
17 6
        return Symmetric\Crypto::encrypt($data, $this->loadKey($key));
18
    }
19
20 3
    public function decrypt($data, Key $key)
21
    {
22 3
        return Symmetric\Crypto::decrypt($data, $this->loadKey($key));
23
    }
24
25 7
    public function getIdentifier(): IdentityInterface
26
    {
27 7
        return new EncryptorIdentity(self::IDENTITY);
28
    }
29
30
    /**
31
     * @return Symmetric\EncryptionKey
32
     * @throws \ParagonIE\Halite\Alerts\CannotPerformOperation
33
     */
34 6
    private function loadKey(Key $key)
35
    {
36 6
        return KeyFactory::loadEncryptionKey($key->getKeyInfo());
37
    }
38
}