DefusePHPEncryptionAdapter   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 24
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 13
dl 0
loc 24
rs 10
c 0
b 0
f 0
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A encrypt() 0 9 2
A decrypt() 0 9 2
1
<?php
2
3
namespace SilverStripe\MFA\Service;
4
5
use Defuse\Crypto\Crypto;
6
use Defuse\Crypto\Exception\CryptoException;
7
use SilverStripe\MFA\Exception\EncryptionAdapterException;
8
9
/**
10
 * An encryption adapter for defuse/php-encryption, enabled by default.
11
 */
12
class DefusePHPEncryptionAdapter implements EncryptionAdapterInterface
13
{
14
    public function encrypt(string $plaintext, string $key): string
15
    {
16
        try {
17
            return Crypto::encryptWithPassword($plaintext, $key);
18
        } catch (CryptoException $exception) {
19
            throw new EncryptionAdapterException(
20
                'Failed to encrypt string with provided key',
21
                0,
22
                $exception
23
            );
24
        }
25
    }
26
27
    public function decrypt(string $ciphertext, string $key): string
28
    {
29
        try {
30
            return Crypto::decryptWithPassword($ciphertext, $key);
31
        } catch (CryptoException $exception) {
32
            throw new EncryptionAdapterException(
33
                'Failed to decrypt cipher text with provided key',
34
                0,
35
                $exception
36
            );
37
        }
38
    }
39
}
40