| 1 |
<?php |
| 2 |
|
| 3 |
namespace Wikimedia\Tests; |
| 4 |
|
| 5 |
use MediaWikiUnitTestCase; |
| 6 |
use MWCryptHash; |
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
class MWCryptHashTest extends MediaWikiUnitTestCase { |
| 14 |
|
| 15 |
public function testHashLength() { |
| 16 |
if ( MWCryptHash::hashAlgo() !== 'whirlpool' ) { |
| 17 |
$this->markTestSkipped( 'Hash algorithm isn\'t whirlpool' ); |
| 18 |
} |
| 19 |
|
| 20 |
$this->assertEquals( 64, MWCryptHash::hashLength(), 'Raw hash length' ); |
| 21 |
$this->assertEquals( 128, MWCryptHash::hashLength( false ), 'Hex hash length' ); |
| 22 |
} |
| 23 |
|
| 24 |
public function testHash() { |
| 25 |
if ( MWCryptHash::hashAlgo() !== 'whirlpool' ) { |
| 26 |
$this->markTestSkipped( 'Hash algorithm isn\'t whirlpool' ); |
| 27 |
} |
| 28 |
|
| 29 |
$data = 'foobar'; |
| 30 |
$hash = '9923afaec3a86f865bb231a588f453f84e8151a2deb4109aebc6de4284be5bebcff4fab82a7e51d920237340a043736e9d13bab196006dcca0fe65314d68eab9'; |
| 31 |
|
| 32 |
$this->assertEquals( |
| 33 |
hex2bin( $hash ), |
| 34 |
MWCryptHash::hash( $data ), |
| 35 |
'Raw hash' |
| 36 |
); |
| 37 |
$this->assertEquals( |
| 38 |
$hash, |
| 39 |
MWCryptHash::hash( $data, false ), |
| 40 |
'Hex hash' |
| 41 |
); |
| 42 |
} |
| 43 |
|
| 44 |
public function testHmac() { |
| 45 |
if ( MWCryptHash::hashAlgo() !== 'whirlpool' ) { |
| 46 |
$this->markTestSkipped( 'Hash algorithm isn\'t whirlpool' ); |
| 47 |
} |
| 48 |
|
| 49 |
$data = 'foobar'; |
| 50 |
$key = 'secret'; |
| 51 |
$hash = 'ddc94177b2020e55ce2049199fd9cc6327f416ff6dc621cc34cb43d9bec61d73372b4790c0e24957f565ecaf2d42821e6303619093e99cbe14a3b9250bda5f81'; |
| 52 |
|
| 53 |
$this->assertEquals( |
| 54 |
hex2bin( $hash ), |
| 55 |
MWCryptHash::hmac( $data, $key ), |
| 56 |
'Raw hmac' |
| 57 |
); |
| 58 |
$this->assertEquals( |
| 59 |
$hash, |
| 60 |
MWCryptHash::hmac( $data, $key, false ), |
| 61 |
'Hex hmac' |
| 62 |
); |
| 63 |
} |
| 64 |
|
| 65 |
} |