Test Failed
Push — newinternal ( 8c4587...b2f220 )
by Michael
15:41 queued 06:17
created

EncryptionHelper::encryptData()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 5
c 1
b 0
f 0
dl 0
loc 9
rs 10
cc 1
nc 1
nop 1
1
<?php
2
/******************************************************************************
3
 * Wikipedia Account Creation Assistance tool                                 *
4
 *                                                                            *
5
 * All code in this file is released into the public domain by the ACC        *
6
 * Development Team. Please see team.json for a list of contributors.         *
7
 ******************************************************************************/
8
9
namespace Waca\Security;
10
11
use Waca\SiteConfiguration;
0 ignored issues
show
Bug introduced by
The type Waca\SiteConfiguration was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
12
13
class EncryptionHelper
14
{
15
    /**
16
     * @var SiteConfiguration
17
     */
18
    private $configuration;
19
20
    /**
21
     * EncryptionHelper constructor.
22
     *
23
     * @param SiteConfiguration $configuration
24
     */
25
    public function __construct(SiteConfiguration $configuration)
26
    {
27
        $this->configuration = $configuration;
28
    }
29
30
    public function encryptData($secret)
31
    {
32
        $iv = openssl_random_pseudo_bytes(16);
33
        $password = $this->getEncryptionKey();
34
        $encryptedKey = openssl_encrypt($secret, 'aes-256-ctr', $password, OPENSSL_RAW_DATA, $iv);
35
36
        $data = base64_encode($iv) . '|' . base64_encode($encryptedKey);
37
38
        return $data;
39
    }
40
41
    public function decryptData($data)
42
    {
43
        list($iv, $encryptedKey) = array_map('base64_decode', explode('|', $data));
44
45
        $password = $this->getEncryptionKey();
46
47
        $secret = openssl_decrypt($encryptedKey, 'aes-256-ctr', $password, OPENSSL_RAW_DATA, $iv);
48
49
        return $secret;
50
    }
51
52
    /**
53
     * @return string
54
     */
55
    private function getEncryptionKey()
56
    {
57
        return openssl_digest($this->configuration->getTotpEncryptionKey(), 'sha256');
58
    }
59
}