Completed
Push — master ( 8fe1b2...42aba3 )
by Michael
03:39
created

OpensslStack::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
1
<?php declare(strict_types=1);
2
3
/**
4
 * OpensslStack.php
5
 *
6
 * PHP version 7
7
 *
8
 * @category Dcrypt
9
 * @package  Dcrypt
10
 * @author   Michael Meyer (mmeyer2k) <[email protected]>
11
 * @license  http://opensource.org/licenses/MIT The MIT License (MIT)
12
 * @link     https://github.com/mmeyer2k/dcrypt
13
 */
14
15
namespace Dcrypt;
16
17
/**
18
 * A factory class to build and use custom encryption stacks.
19
 *
20
 * @category Dcrypt
21
 * @package  Dcrypt
22
 * @author   Michael Meyer (mmeyer2k) <[email protected]>
23
 * @license  http://opensource.org/licenses/MIT The MIT License (MIT)
24
 * @link     https://github.com/mmeyer2k/dcrypt
25
 */
26
class OpensslStack
27
{
28
    /**
29
     * @var array
30
     */
31
    private $stack = [];
32
33
    /**
34
     * @var string
35
     */
36
    private $passkey;
37
38
    /**
39
     * @var int
40
     */
41
    private $cost;
42
43
    /**
44
     * OpensslStack constructor.
45
     *
46
     * @param string $passkey Password or key
47
     * @param int    $cost    Cost when using password mode
48
     */
49
    public function __construct(string $passkey, int $cost = 0)
50
    {
51
        $this->passkey = $passkey;
52
53
        $this->cost = $cost;
54
    }
55
56
    /**
57
     * @param string $cipher
58
     * @param string $algo
59
     * @return OpensslStack
60
     */
61
    public function add(string $cipher, string $algo): self
62
    {
63
        $this->stack[] = [$cipher, $algo];
64
65
        return $this;
66
    }
67
68
    /**
69
     * Encrypt data using custom stack
70
     *
71
     * @param string $data Data to encrypt
72
     * @return string
73
     * @throws \Exception
74
     */
75 View Code Duplication
    public function encrypt(string $data): string
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
76
    {
77
        foreach ($this->stack as $s) {
78
            $data = OpensslStatic::encrypt($data, $this->passkey, $s[0], $s[1], $this->cost);
79
        }
80
81
        return $data;
82
    }
83
84
    /**
85
     * Decrypt data using custom stack
86
     *
87
     * @param string $data Data to decrypt
88
     * @return string
89
     * @throws \Exception
90
     */
91 View Code Duplication
    public function decrypt(string $data): string
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
92
    {
93
        foreach (\array_reverse($this->stack) as $s) {
94
            $data = OpensslStatic::decrypt($data, $this->passkey, $s[0], $s[1], $this->cost);
95
        }
96
97
        return $data;
98
    }
99
}