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); |
|
|
|
|
45
|
|
|
|
46
|
1 |
|
$binaryValue = $this->hex2bin($value); |
|
|
|
|
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); |
|
|
|
|
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
|
|
|
|
If you suppress an error, we recommend checking for the error condition explicitly: