wiki.techinc.nl/tests/phpunit/includes/utils/MWCryptHashTest.php
Kevin Israel 6492c009ef 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
2016-02-20 00:27:24 -05:00

63 lines
1.6 KiB
PHP

<?php
/**
*
* @group Hash
*/
class MWCryptHashTest extends MediaWikiTestCase {
public function testHashLength() {
if ( MWCryptHash::hashAlgo() !== 'whirlpool' ) {
$this->markTestSkipped( 'Hash algorithm isn\'t whirlpool' );
}
$this->assertEquals( 64, MWCryptHash::hashLength(), 'Raw hash length' );
$this->assertEquals( 128, MWCryptHash::hashLength( false ), 'Hex hash length' );
}
public function testHash() {
if ( MWCryptHash::hashAlgo() !== 'whirlpool' ) {
$this->markTestSkipped( 'Hash algorithm isn\'t whirlpool' );
}
$data = 'foobar';
// @codingStandardsIgnoreStart Generic.Files.LineLength
$hash = '9923afaec3a86f865bb231a588f453f84e8151a2deb4109aebc6de4284be5bebcff4fab82a7e51d920237340a043736e9d13bab196006dcca0fe65314d68eab9';
// @codingStandardsIgnoreEnd
$this->assertEquals(
hex2bin( $hash ),
MWCryptHash::hash( $data ),
'Raw hash'
);
$this->assertEquals(
$hash,
MWCryptHash::hash( $data, false ),
'Hex hash'
);
}
public function testHmac() {
if ( MWCryptHash::hashAlgo() !== 'whirlpool' ) {
$this->markTestSkipped( 'Hash algorithm isn\'t whirlpool' );
}
$data = 'foobar';
$key = 'secret';
// @codingStandardsIgnoreStart Generic.Files.LineLength
$hash = 'ddc94177b2020e55ce2049199fd9cc6327f416ff6dc621cc34cb43d9bec61d73372b4790c0e24957f565ecaf2d42821e6303619093e99cbe14a3b9250bda5f81';
// @codingStandardsIgnoreEnd
$this->assertEquals(
hex2bin( $hash ),
MWCryptHash::hmac( $data, $key ),
'Raw hmac'
);
$this->assertEquals(
$hash,
MWCryptHash::hmac( $data, $key, false ),
'Hex hmac'
);
}
}