UrlSafeCipher::encrypt()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
crap 1
1
<?php
2
3
namespace Kunstmaan\UtilitiesBundle\Helper\Cipher;
4
5
/**
6
 * Cipher, this class can be used to encrypt string values to a format which is safe to use in urls.
7
 */
8
class UrlSafeCipher extends Cipher
9
{
10
    /**
11
     * Encrypt the given value so that it's unreadable and that it can be used in an url.
12
     *
13
     * @param string $value
14
     * @param bool   $raw_binary
15
     *
16
     * @return string
17
     *
18
     * @throws \Defuse\Crypto\Exception\EnvironmentIsBrokenException
19
     */
20 1
    public function encrypt($value, $raw_binary = false)
21
    {
22 1
        return bin2hex(parent::encrypt($value, $raw_binary));
23
    }
24
25
    /**
26
     * Decrypt the given value so that it's readable again.
27
     *
28
     * @param string $value
29
     * @param bool   $raw_binary
30
     *
31
     * @return string
32
     *
33
     * @throws \Defuse\Crypto\Exception\WrongKeyOrModifiedCiphertextException
34
     * @throws \Defuse\Crypto\Exception\EnvironmentIsBrokenException
35
     */
36 2
    public function decrypt($value, $raw_binary = false)
37
    {
38 2
        $reflector = new \ReflectionMethod($this, 'hex2bin');
39 2
        $methodOverride = ($reflector->getDeclaringClass()->getName() !== __CLASS__);
40
41
        //NEXT_MAJOR: Remove override check, remove hex2bin method and switch to native php function
42 2
        $binaryValue = hex2bin($value);
43 2
        if ($methodOverride) {
44 1
            @trigger_error(sprintf('Overriding the "%s::hex2bin" method is deprecated since KunstmaanUtilitiesBundle 5.5 and the method will be removed in KunstmaanUtilitiesBundle 6.0. The "%s" class will use the native hex2bin function instead.', __CLASS__, __CLASS__), E_USER_DEPRECATED);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
45
46 1
            $binaryValue = $this->hex2bin($value);
0 ignored issues
show
Deprecated Code introduced by
The method Kunstmaan\UtilitiesBundl...rlSafeCipher::hex2bin() has been deprecated with message: The "Kunstmaan\UtilitiesBundle\Helper\Cipher\UrlSafeCipher::hex2bin" method is deprecated since KunstmaanUtilitiesBundle 5.5 and will be removed in KunstmaanUtilitiesBundle 6.0.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
47
        }
48
49 2
        return parent::decrypt($binaryValue, $raw_binary);
50
    }
51
52
    /**
53
     * Decodes a hexadecimal encoded binary string.
54
     * PHP version >= 5.4 has a function for this by default.
55
     *
56
     * @deprecated The "Kunstmaan\UtilitiesBundle\Helper\Cipher\UrlSafeCipher::hex2bin" method is deprecated since KunstmaanUtilitiesBundle 5.5 and will be removed in KunstmaanUtilitiesBundle 6.0.
57
     *
58
     * @param string $hexString
59
     *
60
     * @return string
61
     */
62 2
    public function hex2bin($hexString)
63
    {
64 2
        @trigger_error(sprintf('The "%s::hex2bin" method is deprecated since KunstmaanUtilitiesBundle 5.5 and will be removed in KunstmaanUtilitiesBundle 6.0.', __CLASS__), E_USER_DEPRECATED);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
65
66 2
        $pos = 0;
67 2
        $result = '';
68 2
        while ($pos < \strlen($hexString)) {
69 2
            if (strpos(" \t\n\r", $hexString[$pos]) !== false) {
70
                ++$pos;
71
            } else {
72 2
                $code = hexdec(substr($hexString, $pos, 2));
73 2
                $pos += 2;
74 2
                $result .= \chr($code);
75
            }
76
        }
77
78 2
        return $result;
79
    }
80
}
81