Merge "Type hint against LinkTarget in WatchedItemStore"
[lhc/web/wiklou.git] / tests / phpunit / unit / includes / utils / MWCryptHashTest.php
1 <?php
2
3 /**
4 * @group Hash
5 *
6 * @covers MWCryptHash
7 */
8 class MWCryptHashTest extends MediaWikiUnitTestCase {
9
10 public function testHashLength() {
11 if ( MWCryptHash::hashAlgo() !== 'whirlpool' ) {
12 $this->markTestSkipped( 'Hash algorithm isn\'t whirlpool' );
13 }
14
15 $this->assertEquals( 64, MWCryptHash::hashLength(), 'Raw hash length' );
16 $this->assertEquals( 128, MWCryptHash::hashLength( false ), 'Hex hash length' );
17 }
18
19 public function testHash() {
20 if ( MWCryptHash::hashAlgo() !== 'whirlpool' ) {
21 $this->markTestSkipped( 'Hash algorithm isn\'t whirlpool' );
22 }
23
24 $data = 'foobar';
25 // phpcs:ignore Generic.Files.LineLength
26 $hash = '9923afaec3a86f865bb231a588f453f84e8151a2deb4109aebc6de4284be5bebcff4fab82a7e51d920237340a043736e9d13bab196006dcca0fe65314d68eab9';
27
28 $this->assertEquals(
29 hex2bin( $hash ),
30 MWCryptHash::hash( $data ),
31 'Raw hash'
32 );
33 $this->assertEquals(
34 $hash,
35 MWCryptHash::hash( $data, false ),
36 'Hex hash'
37 );
38 }
39
40 public function testHmac() {
41 if ( MWCryptHash::hashAlgo() !== 'whirlpool' ) {
42 $this->markTestSkipped( 'Hash algorithm isn\'t whirlpool' );
43 }
44
45 $data = 'foobar';
46 $key = 'secret';
47 // phpcs:ignore Generic.Files.LineLength
48 $hash = 'ddc94177b2020e55ce2049199fd9cc6327f416ff6dc621cc34cb43d9bec61d73372b4790c0e24957f565ecaf2d42821e6303619093e99cbe14a3b9250bda5f81';
49
50 $this->assertEquals(
51 hex2bin( $hash ),
52 MWCryptHash::hmac( $data, $key ),
53 'Raw hmac'
54 );
55 $this->assertEquals(
56 $hash,
57 MWCryptHash::hmac( $data, $key, false ),
58 'Hex hmac'
59 );
60 }
61
62 }