From: Kevin Israel Date: Sat, 20 Feb 2016 01:43:21 +0000 (-0500) Subject: Use hex2bin() instead of pack() X-Git-Tag: 1.31.0-rc.0~7877^2 X-Git-Url: http://git.cyclocoop.org/%22%20.%20generer_url_ecrire%28%22articles_versions%22%2C%22id_article=%24id_article%22%29%20.%20%22?a=commitdiff_plain;h=6492c009ef539f7c3c3f864bab8482aee255a6ab;p=lhc%2Fweb%2Fwiklou.git Use hex2bin() instead of pack() This function was added in PHP 5.4.0 and can be used now that MediaWiki only works with PHP 5.5.9 or higher. Also fixed a bug in ApiQueryCategoryMembers::validateHexSortkey() that allowed a single line feed at the end of the string to pass. Change-Id: I5b577e7dcc5fb6a06ab550429aae657dbcc79083 --- diff --git a/includes/api/ApiQueryCategoryMembers.php b/includes/api/ApiQueryCategoryMembers.php index ef455b49b2..4865ad56f9 100644 --- a/includes/api/ApiQueryCategoryMembers.php +++ b/includes/api/ApiQueryCategoryMembers.php @@ -53,7 +53,7 @@ class ApiQueryCategoryMembers extends ApiQueryGeneratorBase { */ private function validateHexSortkey( $hexSortkey ) { // A hex sortkey has an unbound number of 2 letter pairs - return preg_match( '/^(?:[a-fA-F0-9]{2})*$/', $hexSortkey ); + return preg_match( '/^(?:[a-fA-F0-9]{2})*$/D', $hexSortkey ); } /** @@ -138,8 +138,7 @@ class ApiQueryCategoryMembers extends ApiQueryGeneratorBase { // Add a WHERE clause for sortkey and from $this->dieContinueUsageIf( !$this->validateHexSortkey( $cont[1] ) ); - // pack( "H*", $foo ) is used to convert hex back to binary - $escSortkey = $this->getDB()->addQuotes( pack( 'H*', $cont[1] ) ); + $escSortkey = $this->getDB()->addQuotes( hex2bin( $cont[1] ) ); $from = intval( $cont[2] ); $op = $dir == 'newer' ? '>' : '<'; // $contWhere is used further down @@ -156,7 +155,7 @@ class ApiQueryCategoryMembers extends ApiQueryGeneratorBase { if ( !$this->validateHexSortkey( $params['starthexsortkey'] ) ) { $this->dieUsage( 'The starthexsortkey provided is not valid', 'bad_starthexsortkey' ); } - $startsortkey = pack( 'H*', $params['starthexsortkey'] ); + $startsortkey = hex2bin( $params['starthexsortkey'] ); } else { $startsortkey = $params['startsortkey']; } @@ -166,7 +165,7 @@ class ApiQueryCategoryMembers extends ApiQueryGeneratorBase { if ( !$this->validateHexSortkey( $params['endhexsortkey'] ) ) { $this->dieUsage( 'The endhexsortkey provided is not valid', 'bad_endhexsortkey' ); } - $endsortkey = pack( 'H*', $params['endhexsortkey'] ); + $endsortkey = hex2bin( $params['endhexsortkey'] ); } else { $endsortkey = $params['endsortkey']; } diff --git a/tests/phpunit/includes/utils/MWCryptHKDFTest.php b/tests/phpunit/includes/utils/MWCryptHKDFTest.php index 5dc049831b..17442b8f12 100644 --- a/tests/phpunit/includes/utils/MWCryptHKDFTest.php +++ b/tests/phpunit/includes/utils/MWCryptHKDFTest.php @@ -28,10 +28,10 @@ class MWCryptHKDFTest extends MediaWikiTestCase { * @dataProvider providerRfc5869 */ public function testRfc5869( $hash, $ikm, $salt, $info, $L, $prk, $okm ) { - $ikm = pack( 'H*', $ikm ); - $salt = pack( 'H*', $salt ); - $info = pack( 'H*', $info ); - $okm = pack( 'H*', $okm ); + $ikm = hex2bin( $ikm ); + $salt = hex2bin( $salt ); + $info = hex2bin( $info ); + $okm = hex2bin( $okm ); $result = MWCryptHKDF::HKDF( $hash, $ikm, $salt, $info, $L ); $this->assertEquals( $okm, $result ); } diff --git a/tests/phpunit/includes/utils/MWCryptHashTest.php b/tests/phpunit/includes/utils/MWCryptHashTest.php index ad54e2f39e..4c85c3dc79 100644 --- a/tests/phpunit/includes/utils/MWCryptHashTest.php +++ b/tests/phpunit/includes/utils/MWCryptHashTest.php @@ -26,7 +26,7 @@ class MWCryptHashTest extends MediaWikiTestCase { // @codingStandardsIgnoreEnd $this->assertEquals( - pack( 'H*', $hash ), + hex2bin( $hash ), MWCryptHash::hash( $data ), 'Raw hash' ); @@ -49,7 +49,7 @@ class MWCryptHashTest extends MediaWikiTestCase { // @codingStandardsIgnoreEnd $this->assertEquals( - pack( 'H*', $hash ), + hex2bin( $hash ), MWCryptHash::hmac( $data, $key ), 'Raw hmac' );