Passed
Push — master ( 21ea66...db5c4e )
by Kirill
02:53
created

Encrypter::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 4
c 1
b 0
f 0
dl 0
loc 6
rs 10
cc 2
nc 2
nop 1
1
<?php
2
3
/**
4
 * Spiral Framework.
5
 *
6
 * @license   MIT
7
 * @author    Anton Titov (Wolfy-J)
8
 */
9
10
declare(strict_types=1);
11
12
namespace Spiral\Encrypter;
13
14
use Defuse\Crypto\Crypto;
15
use Defuse\Crypto\Exception\CryptoException;
16
use Defuse\Crypto\Exception\EnvironmentIsBrokenException;
17
use Defuse\Crypto\Key;
18
use Spiral\Core\Container\InjectableInterface;
19
use Spiral\Encrypter\Exception\DecryptException;
20
use Spiral\Encrypter\Exception\EncrypterException;
21
use Spiral\Encrypter\Exception\EncryptException;
22
23
/**
24
 * Default implementation of spiral encrypter. Facade at top of defuse/php-encryption
25
 *
26
 * @see https://github.com/defuse/php-encryption
27
 */
28
final class Encrypter implements EncrypterInterface, InjectableInterface
29
{
30
    public const INJECTOR = EncrypterFactory::class;
31
32
    /** @var Key */
33
    private $key = null;
34
35
    /**
36
     * @param string $key Loads a Key from its encoded form (ANSI).
37
     */
38
    public function __construct(string $key)
39
    {
40
        try {
41
            $this->key = Key::loadFromAsciiSafeString($key);
42
        } catch (CryptoException $e) {
43
            throw new EncrypterException($e->getMessage(), $e->getCode(), $e);
44
        }
45
    }
46
47
    /**
48
     * {@inheritdoc}
49
     */
50
    public function withKey(string $key): EncrypterInterface
51
    {
52
        $encrypter = clone $this;
53
        try {
54
            $encrypter->key = Key::loadFromAsciiSafeString($key);
55
        } catch (CryptoException $e) {
56
            throw new EncrypterException($e->getMessage(), $e->getCode(), $e);
57
        }
58
59
        return $encrypter;
60
    }
61
62
    /**
63
     * {@inheritdoc}
64
     */
65
    public function getKey(): string
66
    {
67
        try {
68
            return $this->key->saveToAsciiSafeString();
69
        } catch (EnvironmentIsBrokenException $e) {
70
            throw new EncrypterException($e->getMessage(), $e->getCode(), $e);
71
        }
72
    }
73
74
    /**
75
     * {@inheritdoc}
76
     *
77
     * Data encoded using json_encode method, only supported formats are allowed!
78
     */
79
    public function encrypt($data): string
80
    {
81
        $packed = json_encode($data);
82
83
        try {
84
            return base64_encode(Crypto::Encrypt($packed, $this->key));
85
        } catch (\Throwable $e) {
86
            throw new EncryptException($e->getMessage(), $e->getCode(), $e);
87
        }
88
    }
89
90
    /**
91
     * {@inheritdoc}
92
     *
93
     * json_decode with assoc flag set to true
94
     */
95
    public function decrypt(string $payload)
96
    {
97
        try {
98
            $result = Crypto::Decrypt(
99
                base64_decode($payload),
100
                $this->key
101
            );
102
103
            return json_decode($result, true);
104
        } catch (\Throwable $e) {
105
            throw new DecryptException($e->getMessage(), $e->getCode(), $e);
106
        }
107
    }
108
}
109