Completed
Push — master ( 41a0f9...045396 )
by Michael
01:30
created

OpenSSL::exceptionIfFalse()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 3
nc 2
nop 1
1
<?php
2
3
/**
4
 * OpenSSL.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
class OpenSSL
18
{
19
    /**
20
     * OpenSSL encrypt wrapper function
21
     *
22
     * @param string $data
23
     * @param string $method
24
     * @param string $key
25
     * @param string $iv
26
     * @return string
27
     */
28
    protected static function openssl_encrypt(string $data, string $method, string $key, string $iv): string
29
    {
30
        $ret = \openssl_encrypt($data, $method, $key, 1, $iv);
31
32
        self::exceptionIfFalse($ret);
33
34
        return $ret;
35
    }
36
37
    /**
38
     * OpenSSL decrypt wrapper function
39
     *
40
     * @param string $data
41
     * @param string $method
42
     * @param string $key
43
     * @param string $iv
44
     * @return string
45
     */
46
    protected static function openssl_decrypt(string $data, string $method, string $key, string $iv): string
47
    {
48
        $ret = \openssl_decrypt($data, $method, $key, 1, $iv);
49
50
        self::exceptionIfFalse($ret);
51
52
        return $ret;
53
    }
54
55
    /**
56
     * Throw an exception if openssl function returns false
57
     *
58
     * @param string $data
59
     * @throws \Exception
60
     */
61
    private static function exceptionIfFalse(string $data)
62
    {
63
        if ($data === false) {
64
            throw new \Exception('OpenSSL failed to encrypt/decrypt message.');
65
        }
66
    }
67
}