Skip to content

Commit

Permalink
Merge 7a0cc1c into ef70088
Browse files Browse the repository at this point in the history
  • Loading branch information
acrobat committed Jan 27, 2020
2 parents ef70088 + 7a0cc1c commit 183d0d6
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
17 changes: 16 additions & 1 deletion src/Kunstmaan/UtilitiesBundle/Helper/Cipher/UrlSafeCipher.php
Expand Up @@ -35,19 +35,34 @@ public function encrypt($value, $raw_binary = false)
*/
public function decrypt($value, $raw_binary = false)
{
return parent::decrypt($this->hex2bin($value), $raw_binary);
$reflector = new \ReflectionMethod($this, 'hex2bin');
$methodOverride = ($reflector->getDeclaringClass()->getName() !== __CLASS__);

//NEXT_MAJOR: Remove override check, remove hex2bin method and switch to native php function
$binaryValue = hex2bin($value);
if ($methodOverride) {
@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);

$binaryValue = $this->hex2bin($value);
}

return parent::decrypt($binaryValue, $raw_binary);
}

/**
* Decodes a hexadecimal encoded binary string.
* PHP version >= 5.4 has a function for this by default.
*
* @deprecated The "Kunstmaan\UtilitiesBundle\Helper\Cipher\UrlSafeCipher::hex2bin" method is deprecated since KunstmaanUtilitiesBundle 5.5 and will be removed in KunstmaanUtilitiesBundle 6.0.
*
* @param string $hexString
*
* @return string
*/
public function hex2bin($hexString)
{
@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);

$pos = 0;
$result = '';
while ($pos < \strlen($hexString)) {
Expand Down
Expand Up @@ -50,6 +50,7 @@ public function testEncryptDecrypt()
}

/**
* @group legacy
* @covers \Kunstmaan\UtilitiesBundle\Helper\Cipher\UrlSafeCipher::hex2bin
*/
public function testHex2bin()
Expand All @@ -59,4 +60,25 @@ public function testHex2bin()
$binValue = $this->cipher->hex2bin($hexValue);
$this->assertEquals($binValue, self::CONTENT);
}

/**
* @group legacy
* @expectedDeprecation Overriding the "Kunstmaan\UtilitiesBundle\Helper\Cipher\UrlSafeCipher::hex2bin" method is deprecated since KunstmaanUtilitiesBundle 5.5 and the method will be removed in KunstmaanUtilitiesBundle 6.0. The "Kunstmaan\UtilitiesBundle\Helper\Cipher\UrlSafeCipher" class will use the native hex2bin function instead.
* @expectedDeprecation The "Kunstmaan\UtilitiesBundle\Helper\Cipher\UrlSafeCipher::hex2bin" method is deprecated since KunstmaanUtilitiesBundle 5.5 and will be removed in KunstmaanUtilitiesBundle 6.0.
*/
public function testHex2binOverride()
{
$encryptedLetter = '6465663530323030326666656464313233623237656563366361666136306139303637663530626436623665356463326134363162326564646339643565353738643631633336663637626562316535353732393337643561656437653464373163643631383134613636303766626231366462376430393530616332346634636264636663663436333730333132303138303561393166616466616562366163613030366561643664';
$decryptedLetter = 'a';
$cipherOveride = new class(self::SECRET) extends UrlSafeCipher {
public function hex2bin($hexString)
{
return parent::hex2bin($hexString);
}
};

$decryptedResult = $cipherOveride->decrypt($encryptedLetter);

$this->assertEquals($decryptedLetter, $decryptedResult);
}
}

0 comments on commit 183d0d6

Please sign in to comment.